Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. #include <js/Initialization.h>
  2. #include <jsapi.h>
  3. #include <jsfriendapi.h>
  4. #include <cassert>
  5. #include <js/Conversions.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10.  
  11. using namespace std;
  12. using namespace JS;
  13. using namespace js;
  14.  
  15. static JSClassOps global_ops = {
  16.     nullptr, nullptr, nullptr, nullptr,
  17.     nullptr, nullptr, nullptr, nullptr,
  18.     nullptr, nullptr,
  19.     JS_GlobalObjectTraceHook
  20. };
  21.  
  22. static JSClass global_class = {
  23.     "global",
  24.     JSCLASS_GLOBAL_FLAGS,
  25.     &global_ops
  26. };
  27.  
  28. static bool read_file(JSContext* cx, unsigned argc, Value* vp)
  29. {
  30.     CallArgs args = CallArgsFromVp(argc, vp);
  31.  
  32.     if (argc < 1)
  33.         return false;
  34.  
  35.     JSString *path = JS::ToString(cx, args[0]);
  36.     int fd = ::open(JS_EncodeString(cx, path), O_RDONLY);
  37.  
  38.     if (fd == -1)
  39.         return false;
  40.  
  41.     struct stat finfo;
  42.     fstat(fd, &finfo);
  43.     size_t len = finfo.st_size;
  44.  
  45.     if (argc > 1) {
  46.         JSString* opt = JS::ToString(cx, args[1]);
  47.  
  48.         if (!opt)
  49.             return false;
  50.  
  51.         bool match = false;
  52.         if (!JS_StringEqualsAscii(cx, opt, "binary", &match))
  53.             return false;
  54.  
  55.         if (match) {
  56.             RootedObject obj(cx);
  57.             obj = JS_NewUint8Array(cx, len);
  58.  
  59.             for(size_t i = 0; i < len; ++i) {
  60.                 uint8_t buf;
  61.                 read(fd, &buf, 1);
  62.                 JS_SetElement(cx, obj, i, buf);
  63.             }
  64.  
  65.             close(fd);
  66.  
  67.             args.rval().setObject(*obj);
  68.  
  69.             return true;
  70.         }
  71.     }
  72.  
  73.     UniqueChars buf(static_cast<char*>(js_malloc(len + 1)));
  74.  
  75.     if (!buf)
  76.         return false;
  77.  
  78.     read(fd, buf.get(), len);
  79.  
  80.     close(fd);
  81.  
  82.     UniqueTwoByteChars ucbuf(JS::LossyUTF8CharsToNewTwoByteCharsZ(cx,
  83.         JS::UTF8Chars(buf.get(), len), &len).get());
  84.  
  85.     args.rval().setString(JS_NewUCStringCopyN(cx, ucbuf.get(), len));
  86.     return true;
  87. }
  88.  
  89. static bool print(JSContext* cx, unsigned argc, Value* vp)
  90. {
  91.     CallArgs args = CallArgsFromVp(argc, vp);
  92.  
  93.     for(int i = 0; i < argc; ++i) {
  94.         JSString *str = JS::ToString(cx, args[i]);
  95.         printf("%s\n", JS_EncodeString(cx, str));
  96.     }
  97.  
  98.     return true;
  99. }
  100.  
  101. static const JSFunctionSpec functions[] = {
  102.     JS_FN("read", read_file, 2, 0),
  103.     JS_FN("print", print, 1, 0)
  104. };
  105.  
  106. int main(int argc, char **argv)
  107. {
  108.     JS_Init();
  109.  
  110.     JSContext *cx = JS_NewContext(JS::DefaultHeapMaxBytes, JS::DefaultNurseryBytes);
  111.     JS_SetNativeStackQuota(cx, 2 * 128 * sizeof(size_t) * 1024);
  112.     js::UseInternalJobQueues(cx);
  113.     assert(JS::InitSelfHostedCode(cx));
  114.  
  115.     {
  116.         JSAutoRequest ar(cx);
  117.         JS::CompartmentOptions options;
  118.         JS::RootedObject global(cx, JS_NewGlobalObject(cx, &global_class,
  119.             nullptr, JS::DontFireOnNewGlobalHook, options));
  120.  
  121.         JS::RootedValue rval(cx);
  122.  
  123.         {
  124.             JSAutoCompartment ac(cx, global);
  125.             JS_InitStandardClasses(cx, global);
  126.  
  127.             JS::CompileOptions opts(cx);
  128.  
  129.             assert(JS_DefineFunctions(cx, global, functions));
  130.  
  131.             JS::RootedValue unused(cx);
  132.             //JS::Evaluate(cx, opts, "/tmp/test.js", &unused);
  133.             RootedScript script(cx);
  134.             assert(JS::Compile(cx, opts, "/tmp/prova.js", &script));
  135.             JS_ExecuteScript(cx, script, &unused);
  136.             js::RunJobs(cx);
  137.  
  138.             if (JS_IsExceptionPending(cx)) {
  139.                 JS::RootedValue exn(cx);
  140.                 JS_GetPendingException(cx, &exn);
  141.                 JS_ClearPendingException(cx);
  142.  
  143.                 js::ErrorReport report(cx);
  144.                 report.init(cx, exn, js::ErrorReport::WithSideEffects);
  145.                 printf("!!! %s\n", report.toStringResult());
  146.             }
  147.  
  148.             JS::RootedScript scriptx(cx);
  149.             /*bool ok = JS::Compile(cx, opts, "/tmp/opencv.js", &scriptx);
  150.  
  151.             assert(ok);
  152.  
  153.             JS::RootedValue unused(cx);
  154.             ok = JS_ExecuteScript(cx, scriptx, &unused);
  155.  
  156.             assert(ok);*/
  157.  
  158.             /*bool ok = JS::Compile(cx, opts, "/tmp/blur.js", &scriptx);
  159.  
  160.             assert(ok);
  161.  
  162.             ok = JS_ExecuteScript(cx, scriptx, &rval);*/
  163.  
  164.             bool ok = JS::Evaluate(cx, opts, "/tmp/blur.js", &rval);
  165.  
  166.             if (JS_IsExceptionPending(cx)) {
  167.                 JS::RootedValue exn(cx);
  168.                 JS_GetPendingException(cx, &exn);
  169.                 JS_ClearPendingException(cx);
  170.  
  171.                 js::ErrorReport report(cx);
  172.                 report.init(cx, exn, js::ErrorReport::WithSideEffects);
  173.                 printf("!!! %s\n", report.toStringResult());
  174.             }
  175.  
  176.             assert(ok);
  177.  
  178.             JSString *str = rval.toString();
  179.  
  180.             printf("%s\n", JS_EncodeString(cx, str));
  181.         }
  182.     }
  183.  
  184.     JS_DestroyContext(cx);
  185.     JS_ShutDown();
  186.  
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement