Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <js/Initialization.h>
- #include <jsapi.h>
- #include <jsfriendapi.h>
- #include <cassert>
- #include <js/Conversions.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- using namespace std;
- using namespace JS;
- using namespace js;
- static JSClassOps global_ops = {
- nullptr, nullptr, nullptr, nullptr,
- nullptr, nullptr, nullptr, nullptr,
- nullptr, nullptr,
- JS_GlobalObjectTraceHook
- };
- static JSClass global_class = {
- "global",
- JSCLASS_GLOBAL_FLAGS,
- &global_ops
- };
- static bool read_file(JSContext* cx, unsigned argc, Value* vp)
- {
- CallArgs args = CallArgsFromVp(argc, vp);
- if (argc < 1)
- return false;
- JSString *path = JS::ToString(cx, args[0]);
- int fd = ::open(JS_EncodeString(cx, path), O_RDONLY);
- if (fd == -1)
- return false;
- struct stat finfo;
- fstat(fd, &finfo);
- size_t len = finfo.st_size;
- if (argc > 1) {
- JSString* opt = JS::ToString(cx, args[1]);
- if (!opt)
- return false;
- bool match = false;
- if (!JS_StringEqualsAscii(cx, opt, "binary", &match))
- return false;
- if (match) {
- RootedObject obj(cx);
- obj = JS_NewUint8Array(cx, len);
- for(size_t i = 0; i < len; ++i) {
- uint8_t buf;
- read(fd, &buf, 1);
- JS_SetElement(cx, obj, i, buf);
- }
- close(fd);
- args.rval().setObject(*obj);
- return true;
- }
- }
- UniqueChars buf(static_cast<char*>(js_malloc(len + 1)));
- if (!buf)
- return false;
- read(fd, buf.get(), len);
- close(fd);
- UniqueTwoByteChars ucbuf(JS::LossyUTF8CharsToNewTwoByteCharsZ(cx,
- JS::UTF8Chars(buf.get(), len), &len).get());
- args.rval().setString(JS_NewUCStringCopyN(cx, ucbuf.get(), len));
- return true;
- }
- static bool print(JSContext* cx, unsigned argc, Value* vp)
- {
- CallArgs args = CallArgsFromVp(argc, vp);
- for(int i = 0; i < argc; ++i) {
- JSString *str = JS::ToString(cx, args[i]);
- printf("%s\n", JS_EncodeString(cx, str));
- }
- return true;
- }
- static const JSFunctionSpec functions[] = {
- JS_FN("read", read_file, 2, 0),
- JS_FN("print", print, 1, 0)
- };
- int main(int argc, char **argv)
- {
- JS_Init();
- JSContext *cx = JS_NewContext(JS::DefaultHeapMaxBytes, JS::DefaultNurseryBytes);
- JS_SetNativeStackQuota(cx, 2 * 128 * sizeof(size_t) * 1024);
- js::UseInternalJobQueues(cx);
- assert(JS::InitSelfHostedCode(cx));
- {
- JSAutoRequest ar(cx);
- JS::CompartmentOptions options;
- JS::RootedObject global(cx, JS_NewGlobalObject(cx, &global_class,
- nullptr, JS::DontFireOnNewGlobalHook, options));
- JS::RootedValue rval(cx);
- {
- JSAutoCompartment ac(cx, global);
- JS_InitStandardClasses(cx, global);
- JS::CompileOptions opts(cx);
- assert(JS_DefineFunctions(cx, global, functions));
- JS::RootedValue unused(cx);
- //JS::Evaluate(cx, opts, "/tmp/test.js", &unused);
- RootedScript script(cx);
- assert(JS::Compile(cx, opts, "/tmp/prova.js", &script));
- JS_ExecuteScript(cx, script, &unused);
- js::RunJobs(cx);
- if (JS_IsExceptionPending(cx)) {
- JS::RootedValue exn(cx);
- JS_GetPendingException(cx, &exn);
- JS_ClearPendingException(cx);
- js::ErrorReport report(cx);
- report.init(cx, exn, js::ErrorReport::WithSideEffects);
- printf("!!! %s\n", report.toStringResult());
- }
- JS::RootedScript scriptx(cx);
- /*bool ok = JS::Compile(cx, opts, "/tmp/opencv.js", &scriptx);
- assert(ok);
- JS::RootedValue unused(cx);
- ok = JS_ExecuteScript(cx, scriptx, &unused);
- assert(ok);*/
- /*bool ok = JS::Compile(cx, opts, "/tmp/blur.js", &scriptx);
- assert(ok);
- ok = JS_ExecuteScript(cx, scriptx, &rval);*/
- bool ok = JS::Evaluate(cx, opts, "/tmp/blur.js", &rval);
- if (JS_IsExceptionPending(cx)) {
- JS::RootedValue exn(cx);
- JS_GetPendingException(cx, &exn);
- JS_ClearPendingException(cx);
- js::ErrorReport report(cx);
- report.init(cx, exn, js::ErrorReport::WithSideEffects);
- printf("!!! %s\n", report.toStringResult());
- }
- assert(ok);
- JSString *str = rval.toString();
- printf("%s\n", JS_EncodeString(cx, str));
- }
- }
- JS_DestroyContext(cx);
- JS_ShutDown();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement