Skip to content

Instantly share code, notes, and snippets.

@siscia
Created June 13, 2015 05:32
Show Gist options
  • Select an option

  • Save siscia/4e1b16e4d2cb1eacefa8 to your computer and use it in GitHub Desktop.

Select an option

Save siscia/4e1b16e4d2cb1eacefa8 to your computer and use it in GitHub Desktop.
#include "jsapi.h"
#include <stdio.h>
#include <fstream>
#include <iostream>
using namespace JS;
// The class of the global object.
static JSClass globalClass = {
"global",
JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub,
JS_DeletePropertyStub,
JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
nullptr, nullptr, nullptr, nullptr,
JS_GlobalObjectTraceHook
};
static const JSClass env_class = globalClass;
int main(int argc, const char *argv[]) {
// Initialize the JS engine.
if (!JS_Init())
return 1;
// Create a JS runtime.
JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L, JS_USE_HELPER_THREADS);
if (!rt)
return 1;
// Create a context.
JSContext *cx;
cx = JS_NewContext(rt, 8195);
if (!cx)
return 1;
int saveLength = 322;
std::ifstream f;
f.open("bytecode", std::ios::binary);
char * loadBuffer = (char *) malloc(sizeof(char) * saveLength);
f.read(loadBuffer, saveLength);
f.close();
RootedObject glob(cx);
{
JSAutoCompartment ac(cx, glob);
RootedScript script(cx);
CompileOptions options(cx);
{
JS::AutoSaveContextOptions asco(cx);
JS::ContextOptionsRef(cx).setNoScriptRval(options.noScriptRval);
script = JS_DecodeScript(cx, loadBuffer, saveLength, options.originPrincipals(cx));
}
}
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment