Ejemplo n.º 1
0
virtual JSRuntime * createRuntime() {
    JSRuntime *rt = JS_NewRuntime(768 * 1024, JS_USE_HELPER_THREADS);
    if (!rt)
        return nullptr;
    setNativeStackQuota(rt);
    return rt;
}
void TraceMonkeyEngine::init()
{
    Logging::log(Logging::DEBUG, "TraceMonkeyEngine::init\r\n");
    INDENT_LOG(Logging::DEBUG);

    assert(runtime == NULL);

    runtime = JS_NewRuntime(8L * 1024L * 1024L); // Force GC after X MB.
    if (runtime == NULL)
    {
        Logging::log(Logging::ERROR, "Cannot create TraceMonkey runtime\r\n");
        assert(0);
    }

    /* Create a context. */
    context = JS_NewContext(runtime, 8192);
    if (context == NULL)
    {
        Logging::log(Logging::ERROR, "Cannot create TraceMonkey runtime\r\n");
        assert(0);
    }

    JS_SetOptions(context, JSOPTION_VAROBJFIX);
    JS_SetVersion(context, JSVERSION_ECMA_3);
    JS_SetErrorReporter(context, reportError);

    // Create the global object. Store it locally for a while until it is usable by others
    JSObject* _global = JS_NewObject(context, &global_class, NULL, NULL);
    if (_global == NULL)
    {
        Logging::log(Logging::ERROR, "Cannot create TraceMonkey runtime\r\n");
        assert(0);
    }

    /* Populate the global object with the standard globals,
       like Object and Array. */
    if (!JS_InitStandardClasses(context, _global))
    {
        Logging::log(Logging::ERROR, "Cannot create TraceMonkey runtime\r\n");
        assert(0);
    }

    // Create our internal wrappers

    globalValue = ScriptValuePtr(new TraceMonkeyValue( this, true, OBJECT_TO_JSVAL(_global) ));
    global = _global; // Now that globalValue is set, it is usable by others, so reveal it

    // JITting
//    JS_ToggleOptions(context, JSOPTION_JIT); // Might be buggy, wait for 1.8 to release

// JS_MaybeGC - call during idle time in the main loop? Or other method?

// Use JS_SetContextPrivate and JS_GetContextPrivate to associate application-specific data with a context.
//  - Useful for security checks, once we have a context for the system and sandboxed contexts
//    for user scripts

    // Debugging features

    JS_SetGCZeal(context, 2); // XXX This is 'extremely high' - make a parameter, see MDC docs
}
JSInterpreter::JSInterpreter() {
        /* Create a JS runtime. You always need at least one runtime per process. */
        rt = JS_NewRuntime(8 * 1024 * 1024);
        if (rt == NULL)
            throw * new std::runtime_error("Can't create JS runtime.");

        /*
         * Create a context. You always need a context per thread.
         * Note that this program is not multi-threaded.
         */
        cx = JS_NewContext(rt, 8192);
        if (cx == NULL)
            throw * new std::runtime_error("Can't create js context.");

        JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
        JS_SetVersion(cx, JSVERSION_LATEST);
        JS_SetErrorReporter(cx, reportError);

        /*
         * Create the global object in a new compartment.
         * You always need a global object per context.
         */
        global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
        if (global == NULL)
            throw * new std::runtime_error("Can't create global object.");

        /*
         * Populate the global object with the standard JavaScript
         * function and object classes, such as Object, Array, Date.
         */
        if (!JS_InitStandardClasses(cx, global))
            throw * new std::runtime_error("Can't initialise standard classes.");

    }
Ejemplo n.º 4
0
bool round_js_sm_engine_init(RoundJavaScriptEngine* engine)
{
  if (!engine)
    return NULL;

  JS_SetCStringsAreUTF8();

  engine->cx = NULL;
  engine->rt = NULL;
  engine->obj = NULL;

  engine->rt = JS_NewRuntime(8L * 1024L * 1024L);
  if (!engine->rt)
    return false;

  engine->cx = JS_NewContext(engine->rt, 8192);
  if (!engine->cx)
    return false;

  JS_SetErrorReporter(engine->cx, RoundJSReportError);

  // Obsolete since JSAPI 16
  engine->obj = JS_NewCompartmentAndGlobalObject(engine->cx, &RoundJSGlobalClass, NULL);
  if (!engine->obj)
    return false;

  JS_InitStandardClasses(engine->cx, engine->obj);
  JS_DefineFunctions(engine->cx, engine->obj, JS_SM_FUNCTIONS);

  return true;
}
Ejemplo n.º 5
0
static int
js_init(void)
{
  JSContext *cx;

  JS_SetCStringsAreUTF8();

  runtime = JS_NewRuntime(0x1000000);

  cx = js_newctx(err_reporter);

  JS_BeginRequest(cx);

  showtimeobj = JS_NewObject(cx, &showtime_class, NULL, NULL);
  JS_DefineFunctions(cx, showtimeobj, showtime_functions);

  JSFunction *fn = JS_DefineFunction(cx, showtimeobj, "RichText",
				     js_RichText, 1, 0);
  RichText = JS_GetFunctionObject(fn);
	     
  JS_AddNamedRoot(cx, &showtimeobj, "showtime");

  JS_EndRequest(cx);
  JS_DestroyContext(cx);

  return 0;
}
Ejemplo n.º 6
0
int main(int argc, const char *argv[]) {
    /* Initialize the JS engine -- new/required as of SpiderMonkey 31. */
    /*if (!JS_Init())
       return 1;*/

    /* Create a JS runtime. */
    JSRuntime *rt = JS_NewRuntime(8L * 1024L * 1024L, JS_NO_HELPER_THREADS);
    if (!rt)
       return 1;

    /* Create a context. */
    JSContext *cx = JS_NewContext(rt, 8192);
    if (!cx)
       return 1;
    JS_SetOptions(cx, JSOPTION_VAROBJFIX);
    JS_SetErrorReporter(cx, reportError);

    int status = run(cx);

    JS_DestroyContext(cx);
    JS_DestroyRuntime(rt);

    /* Shut down the JS engine. */
    JS_ShutDown();

    return status;
}
Ejemplo n.º 7
0
spidermonkey_vm *sm_initialize(long thread_stack, long heap_size) {
  spidermonkey_vm *vm = ejs_alloc(sizeof(spidermonkey_vm));
  spidermonkey_state *state = ejs_alloc(sizeof(spidermonkey_state));
  state->branch_count = 0;
  state->error = NULL;
  state->terminate = 0;
  int gc_size = (int) heap_size * 0.25;
  vm->runtime = JS_NewRuntime(MAX_GC_SIZE);
  JS_SetGCParameter(vm->runtime, JSGC_MAX_BYTES, heap_size);
  JS_SetGCParameter(vm->runtime, JSGC_MAX_MALLOC_BYTES, gc_size);
  vm->context = JS_NewContext(vm->runtime, 8192);
  JS_SetScriptStackQuota(vm->context, thread_stack);

  begin_request(vm);
  JS_SetOptions(vm->context, JSOPTION_VAROBJFIX);
  JS_SetOptions(vm->context, JSOPTION_STRICT);
  JS_SetOptions(vm->context, JSOPTION_COMPILE_N_GO);
  JS_SetOptions(vm->context, JSVERSION_LATEST);
  vm->global = JS_NewCompartmentAndGlobalObject(vm->context, &global_class, NULL);
  JS_InitStandardClasses(vm->context, vm->global);
  JS_SetErrorReporter(vm->context, on_error);
  JS_SetOperationCallback(vm->context, on_branch);
  JS_SetContextPrivate(vm->context, state);
  JSNative funptr = (JSNative) &js_log;
  JS_DefineFunction(vm->context, JS_GetGlobalObject(vm->context), "ejsLog", funptr,
                    0, 0);
  end_request(vm);

  return vm;
}
Ejemplo n.º 8
0
PyObject*
Runtime_new(PyTypeObject* type, PyObject* args, PyObject* kwargs)
{
    Runtime* self = NULL;
    unsigned int stacksize = 0x2000000; // 32 MiB heap size.

    if (!PyArg_ParseTuple(args, "|I", &stacksize)) goto error;

#ifdef SPIDERMONKEY_31
    if (!JS_Init())
    {
        PyErr_SetString(JSError, "Failed to initilaize JS engine");
        goto error;
    }
#endif

    self = (Runtime*) type->tp_alloc(type, 0);
    if(self == NULL) goto error;

    self->rt = JS_NewRuntime(stacksize, JS_NO_HELPER_THREADS);
    if(self->rt == NULL)
    {
        PyErr_SetString(JSError, "Failed to allocate new JSRuntime.");
        goto error;
    }

    goto success;

error:
    Py_XDECREF(self);
    self = NULL;

success:
    return (PyObject*) self;
}
Ejemplo n.º 9
0
Archivo: js.c Proyecto: Hr-/showtime
static int
js_init(void)
{
  JSContext *cx;
  jsval val;

  JS_SetCStringsAreUTF8();

  runtime = JS_NewRuntime(0x1000000);

  cx = js_newctx(err_reporter);

  JS_BeginRequest(cx);

  showtimeobj = JS_NewObject(cx, &showtime_class, NULL, NULL);
  JS_DefineFunctions(cx, showtimeobj, showtime_functions);

  val = INT_TO_JSVAL(showtime_get_version_int());
  JS_SetProperty(cx, showtimeobj, "currentVersionInt", &val);

  val = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, htsversion));
  JS_SetProperty(cx, showtimeobj, "currentVersionString", &val);


  JSFunction *fn = JS_DefineFunction(cx, showtimeobj, "RichText",
				     js_RichText, 1, 0);
  RichText = JS_GetFunctionObject(fn);
	     
  JS_AddNamedRoot(cx, &showtimeobj, "showtime");

  JS_EndRequest(cx);
  JS_DestroyContext(cx);

  return 0;
}
Ejemplo n.º 10
0
int theMain(int argc, char **argv){
	obj.screenLog = TEMP_EN_LOG; //initially disable logging to screen.
		
	//Define the quit handler;
	signal(SIGTERM,exitHandle);
	initSDL();


	//JS_Init(); //SETUP SPIDERMONKEY -> NEW VERSION!!

	if(!(runtime = JS_NewRuntime(8L *1024*1024L * 8,JS_USE_HELPER_THREADS /*JS_NO_HELPER_THREADS*/ ))){
		fprint(stderr,"Could not setup runtime\n");
		exit(EXIT_FAILURE);
	}
	
	init_video_libraries(&argc,&argv);
	
	while(1){
		char *appPath = NULL;
		appPath = launchApp(MENU_SCRIPT,1);
		if(appPath == NULL)
			continue;
		loadApp(appPath);
	}
	return EXIT_SUCCESS;
}
Ejemplo n.º 11
0
int
main (int argc, char *argv[])
{
    JSRuntime *rt;
    JSObject *glob;
    int number_failed;
    Suite *s;
    SRunner *sr;

    g_type_init ();
    
    rt = JS_NewRuntime(0x100000); 
    cx = JS_NewContext(rt, 0x1000); 

    /* create the global object here */
    glob = JS_NewObject(cx, NULL, NULL, NULL);
    JS_InitStandardClasses(cx, glob);

    s = gom_value_suite ();
    sr = srunner_create (s);

    srunner_run_all (sr, CK_NORMAL);
    number_failed = srunner_ntests_failed (sr);
    srunner_free (sr);

    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
Ejemplo n.º 12
0
void SetupScripting() {
    /* Initialize the JS engine -- new/required as of SpiderMonkey 31. */
    JS_Init(120000);

    /* Create a JS runtime. */
    rt = JS_NewRuntime(8L * 1024L * 1024L);
    
    /* Create a context. */
	cx = JS_NewContext(rt, 8192);
    JS_SetOptions(cx, JSOPTION_VAROBJFIX);
    JS_SetErrorReporter(cx, reportError);


	/* Enter a request before running anything in the context */
    JSAutoRequest ar(cx);

    /* Create the global object in a new compartment. */
    global = JS_NewGlobalObject(cx, &global_class, NULL);
    
    /* Set the context's global */
    JSAutoCompartment ac(cx, global);
    JS_SetGlobalObject(cx, global);

    /* Populate the global object with the standard globals, like Object and Array. */
    JS_InitStandardClasses(cx, global);
	
}
Ejemplo n.º 13
0
void JavaScriptEngine::Start() {

	globalRuntime = JS_NewRuntime(32L * 1024L * 1024L);
	if (globalRuntime == NULL) {
		LogInfo(TAG, "(globalRuntime == NULL)");
		return;
	}
	globalContext = JS_NewContext(globalRuntime, 8192);
	if (globalContext == NULL) {
		LogInfo(TAG, "(globalContext == NULL)");
		return;
	}
	JS_SetOptions(globalContext, JSOPTION_VAROBJFIX);
	JS_SetVersion(globalContext, JSVERSION_LATEST);
	JS_SetErrorReporter(globalContext, ReportError);
	globalObject = JS_NewCompartmentAndGlobalObject(globalContext,
			&globalClass, NULL);
	if (globalObject == NULL) {
		LogInfo(TAG, "(globalObject == NULL)");
		return;
	}
	if (!JS_InitStandardClasses(globalContext, globalObject)) {
		LogInfo(TAG, "(!JS_InitStandardClasses(globalContext, global))");
		return;
	}
	BindingGlobalObject();
	BindingObject();
	RunScript("index.js");
}
int main(int argc, const char *argv[]) {
    /* JS variables. */
    JSRuntime *rt;
    JSContext *cx;
    JSObject *global;

    /* Create a JS runtime. */
    rt = JS_NewRuntime(8L * 1024L * 1024L,JS_NO_HELPER_THREADS);
    if (rt == NULL)
       return 1;

    /* Create a context. */
    cx = JS_NewContext(rt, 8192);
    if (cx == NULL)
       return 1;
    JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_METHODJIT);
    JS_SetVersion(cx, JSVERSION_LATEST);
    JS_SetErrorReporter(cx, reportError);

    /* Create the global object in a new compartment. */
    global = JS_NewGlobalObject(cx, &global_class, NULL);
    if (global == NULL)
       return 1;

    /* Populate the global object with the standard globals, like Object and Array. */
    if (!JS_InitStandardClasses(cx, global))
       return 1;

 
    /* Your application code here. This may include JSAPI calls
     * to create your own custom JavaScript objects and to run scripts.
     *
     * The following example code creates a literal JavaScript script,
     * evaluates it, and prints the result to stdout.
     *
     * Errors are conventionally saved in a JSBool variable named ok.
     */
	
	JS_DefineFunction(cx,global,"print",&js_print,0,0);
	
	// execJsFile(cx,"t.js");
	
	// testObj(cx);
	
	beforeTest(cx);
	execJsFile(cx,"t4.js");
	test(cx);
    
 
    /* End of your application code */
 
    /* Clean things up and shut down SpiderMonkey. */
    JS_DestroyContext(cx);
    JS_DestroyRuntime(rt);
    JS_ShutDown();
    return 0;
}
Ejemplo n.º 15
0
virtual JSRuntime * createRuntime() override
{
    JSRuntime* rt = JS_NewRuntime(0);
    if (!rt)
        return nullptr;
    JS_SetGCParameter(rt, JSGC_MAX_BYTES, (uint32_t)-1);
    setNativeStackQuota(rt);
    return rt;
}
Ejemplo n.º 16
0
bool Engine::init()
{
	m_runtime = JS_NewRuntime(8L*1024L*1024L);
	if ( m_runtime==NULL ) {
		return false;
	}

	return true;
}
Ejemplo n.º 17
0
qq_js_t* qq_js_init()
{
    qq_js_t* h =(qq_js_t*) s_malloc0(sizeof(*h));
    h->runtime = JS_NewRuntime(8L*1024L*1024L);
    h->context = JS_NewContext(h->runtime, 8*1024);
    JS_SetOptions(h->context, JSOPTION_VAROBJFIX);
    JS_SetErrorReporter(h->context, report_error);
    h->global = JS_NewCompartmentAndGlobalObject(h->context, &global_class, NULL);
    JS_InitStandardClasses(h->context, h->global);
    return h;
}
Ejemplo n.º 18
0
// Initialize PAC parser.
//
// - Initializes JavaScript engine,
// - Exports dns_functions (defined above) to JavaScript context.
// - Sets error reporting function to print_jserror,
// - Evaluates JavaScript code in pacUtils variable defined in pac_utils.h.
int                                     // 0 (=Failure) or 1 (=Success)
pacparser_init()
{
  jsval rval;
  char *error_prefix = "pacparser.c: pacparser_init:";
  // Initialize JS engine
  if (!(rt = JS_NewRuntime(8L * 1024L * 1024L)) ||
      !(cx = JS_NewContext(rt, 8192)) ||
      !(global = JS_NewObject(cx, &global_class, NULL, NULL)) ||
      !JS_InitStandardClasses(cx, global)) {
    print_error("%s %s\n", error_prefix, "Could not initialize  JavaScript "
		  "runtime.");
    return 0;
  }
  JS_SetErrorReporter(cx, print_jserror);
  // Export our functions to Javascript engine
  if (!JS_DefineFunction(cx, global, "dnsResolve", dns_resolve, 1, 0)) {
    print_error("%s %s\n", error_prefix,
		  "Could not define dnsResolve in JS context.");
    return 0;
  }
  if (!JS_DefineFunction(cx, global, "myIpAddress", my_ip, 0, 0)) {
    print_error("%s %s\n", error_prefix,
		  "Could not define myIpAddress in JS context.");
    return 0;
  }
  if (!JS_DefineFunction(cx, global, "dnsResolveEx", dns_resolve_ex, 1, 0)) {
    print_error("%s %s\n", error_prefix,
      "Could not define dnsResolveEx in JS context.");
    return 0;
  }
  if (!JS_DefineFunction(cx, global, "myIpAddressEx", my_ip_ex, 0, 0)) {
    print_error("%s %s\n", error_prefix,
		  "Could not define myIpAddressEx in JS context.");
    return 0;
  }
  // Evaluate pacUtils. Utility functions required to parse pac files.
  if (!JS_EvaluateScript(cx,           // JS engine context
                         global,       // global object
                         pacUtils,     // this is defined in pac_utils.h
                         strlen(pacUtils),
                         NULL,         // filename (NULL in this case)
                         1,            // line number, used for reporting.
                         &rval)) {
    print_error("%s %s\n", error_prefix,
		  "Could not evaluate pacUtils defined in pac_utils.h.");
    return 0;
  }
  if (_debug()) print_error("DEBUG: Pacparser Initalized.\n");
  return 1;
}
Ejemplo n.º 19
0
bool CompileFile(const std::string &inputFilePath, const std::string &outputFilePath) {
    bool result = false;
    std::string ofp;
    if (!outputFilePath.empty()) {
        ofp = outputFilePath;
    }
    else {
        ofp = RemoveFileExt(inputFilePath) + BYTE_CODE_FILE_EXT;
    }
    std::cout << "Input file: " << inputFilePath << std::endl;
    JSRuntime * runtime = JS_NewRuntime(10 * 1024 * 1024, JS_NO_HELPER_THREADS);
    JSContext *context = JS_NewContext(runtime, 10240);
    JS_SetOptions(context, JSOPTION_TYPE_INFERENCE);
    JS_SetVersion(context, JSVERSION_LATEST);
    JS_SetOptions(context, JS_GetOptions(context) & ~JSOPTION_METHODJIT);
    JS_SetOptions(context, JS_GetOptions(context) & ~JSOPTION_METHODJIT_ALWAYS);
	JSObject* global = JS_NewGlobalObject(context, &GlobalClass, NULL);
    JS_SetErrorReporter(context, &ReportError);
	if (JS_InitStandardClasses(context, global)) {

        JS::CompileOptions options(context);
        options.setUTF8(true);
        options.setSourcePolicy(JS::CompileOptions::NO_SOURCE);
        js::RootedObject rootedObject(context, global);
        std::cout << "Compiling ..." << std::endl;
        JSScript *script = JS::Compile(context, rootedObject, options, inputFilePath.c_str());
        if (script) {
            void *data = NULL;
            uint32_t length = 0;
            std::cout << "Encoding ..." << std::endl;
            data = JS_EncodeScript(context, script, &length);
            if (data) {
                if (WriteFile(ofp, data, length)) {
                    std::cout << "Done! " << "Output file: " << ofp << std::endl;
                    result = true;
                }
            }
        }
        
    }
    if (context) {
        JS_DestroyContext(context);
        context = NULL;
    }
    if (runtime) {
        JS_DestroyRuntime(runtime);
        runtime = NULL;
    }
    return result;
}
Ejemplo n.º 20
0
MozJSImplScope::MozRuntime::MozRuntime() {
    mongo::sm::reset(kMallocMemoryLimit);

    // If this runtime isn't running on an NSPR thread, then it is
    // running on a mongo thread. In that case, we need to insert a
    // fake NSPR thread so that the SM runtime can call PR functions
    // without falling over.
    auto thread = PR_GetCurrentThread();
    if (!thread) {
        PR_BindThread(_thread = PR_CreateFakeThread());
    }

    {
        stdx::unique_lock<stdx::mutex> lk(gRuntimeCreationMutex);

        if (gFirstRuntimeCreated) {
            // If we've already made a runtime, just proceed
            lk.unlock();
        } else {
            // If this is the first one, hold the lock until after the first
            // one's done
            gFirstRuntimeCreated = true;
        }

        _runtime = JS_NewRuntime(kMaxBytesBeforeGC);

        const StackLocator locator;
        const auto available = locator.available();
        if (available) {
            // We fudge by 64k for a two reasons. First, it appears
            // that the internal recursion checks that SM performs can
            // have stack usage between checks of more than 32k in
            // some builds. Second, some platforms report the guard
            // page (in the linux sense) as "part of the stack", even
            // though accessing that page will fault the process. We
            // don't have a good way of getting information about the
            // guard page on those platforms.
            //
            // TODO: What if we are running on a platform with very
            // large pages, like 4MB?
            JS_SetNativeStackQuota(_runtime, available.get() - (64 * 1024));
        }
    }

    uassert(ErrorCodes::JSInterpreterFailure, "Failed to initialize JSRuntime", _runtime);

    _context = JS_NewContext(_runtime, kStackChunkSize);
    uassert(ErrorCodes::JSInterpreterFailure, "Failed to initialize JSContext", _context);
}
Ejemplo n.º 21
0
/* XXX FIXME: Iargv/Ienviron are now associated with running. */
rpmjs rpmjsNew(char ** av, uint32_t flags)
{
    rpmjs js =
#ifdef	NOTYET
	(flags & 0x80000000) ? rpmjsI() :
#endif
	rpmjsGetPool(_rpmjsPool);
    JSI_t I = NULL;

#if defined(WITH_GPSEE)

#if defined(XXX_GPSEE_DEBUGGER)	/* XXX js->jsdc? */
    JSDContext *jsdc;
#endif

    if (flags == 0)
	flags = _rpmjs_options;

    if (F_ISSET(flags, NOUTF8) || getenv("GPSEE_NO_UTF8_C_STRINGS")) {
	JS_DestroyRuntime(JS_NewRuntime(1024));
	putenv((char *) "GPSEE_NO_UTF8_C_STRINGS=1");
    }

    /* XXX FIXME: js->Iargv/js->Ienviron for use by rpmjsRunFile() */
    I = gpsee_createInterpreter();
#if defined(XXX_GPSEE_DEBUGGER)
    js->jsdc = gpsee_initDebugger(I->cx, I->realm, DEBUGGER_JS);
#endif

#ifdef	NOTYET	/* FIXME: dig out where NOCACHE has moved. */
    if (F_ISSET(flags, NOCACHE))
	I->useCompilerCache = 0;
#endif
    if (F_ISSET(flags, NOWARN)) {
	gpsee_runtime_t * grt = JS_GetRuntimePrivate(JS_GetRuntime(I->cx));
	grt->errorReport |= er_noWarnings;
    }

    JS_SetOptions(I->cx, (flags & 0xffff));
#if defined(JS_GC_ZEAL)
    JS_SetGCZeal(I->cx, _rpmjs_zeal);
#endif
#endif	/* WITH_GPSEE */

    js->flags = flags;
    js->I = I;

    return rpmjsLink(js);
}
Ejemplo n.º 22
0
JsExecutionContext::JsExecutionContext(JsParser *jsParser)
{
  parser = jsParser;
  /* Create a new runtime environment. */
  rt = JS_NewRuntime(8L * 1024L * 1024L);
  if (!rt) {
    error("JsParser :: error creating runtime");
    return; /* XXX should return int or ptr! */
  }

  /* Create a new context. */
  cx = JS_NewContext(rt, STACK_CHUNK_SIZE);
  /* if global_context does not have a value, end the program here */
  if (cx == NULL) {
    error("JsParser :: error creating context");
    return;
  }

  JS_BeginRequest(cx);
  // Store a reference to ourselves in the context ...
  JS_SetContextPrivate(cx, parser);

  /* Set a more strict error checking */
  JS_SetOptions(cx, JSOPTION_VAROBJFIX); // | JSOPTION_STRICT);

  /* Set the branch callback */
#if defined JSOPTION_NATIVE_BRANCH_CALLBACK
  JS_SetBranchCallback(cx, js_static_branch_callback);
#else
  JS_SetOperationCallback(cx, js_static_branch_callback);
#endif

  /* Set the error reporter */
  JS_SetErrorReporter(cx, js_error_reporter);

  /* Create the global object here */
  //  JS_SetGlobalObject(global_context, global_object);
  //  this is done in init_class / JS_InitStandardClasses.
  obj = JS_NewObject(cx, &global_class, NULL, NULL);
  init_class();
  JS_EndRequest(cx);
  // deassociate this context from the creating thread
  // so that it can be used in other threads
  // https://developer.mozilla.org/en/SpiderMonkey/JSAPI_Reference/JS_NewContext
  JS_ClearContextThread(cx);
  /** register SIGINT signal */
  //   signal(SIGINT, js_sigint_handler);
}
Ejemplo n.º 23
0
JSRuntime * DLLCALL jsrt_GetNew(int maxbytes, unsigned long timeout, const char *filename, long line)
{
	JSRuntime *ret;

	if(!initialized) {
		initialized=TRUE;
		pthread_mutex_init(&jsrt_mutex, NULL);
		listInit(&rt_list, 0);
		_beginthread(trigger_thread, TRIGGER_THREAD_STACK_SIZE, NULL);
	}
	pthread_mutex_lock(&jsrt_mutex);
	ret=JS_NewRuntime(maxbytes);
	listPushNode(&rt_list, ret);
	pthread_mutex_unlock(&jsrt_mutex);
	return ret;
}
Ejemplo n.º 24
0
Archivo: flim.c Proyecto: mrdg/flim
void initialize_js(struct flim *flim)
{
    flim->js_runtime = JS_NewRuntime(8L * 1024L * 1024L);
    flim->js_context = JS_NewContext(flim->js_runtime, 8192);
    JS_SetOptions(flim->js_context, JSOPTION_VAROBJFIX | JSOPTION_METHODJIT);
    JS_SetVersion(flim->js_context, JSVERSION_LATEST);
    JS_SetErrorReporter(flim->js_context, report_error);

    flim->global = JS_NewCompartmentAndGlobalObject(
                                    flim->js_context, &global_class, NULL);
    JS_InitStandardClasses(flim->js_context, flim->global);
    JS_DefineFunctions(flim->js_context, flim->global, js_functions);

    eval_file("runtime/flim.js");
    eval_file("runtime/underscore.js");
}
Ejemplo n.º 25
0
Archivo: lwjs.c Proyecto: fsky/webqq
lwqq_js_t* lwqq_js_init()
{
    lwqq_js_t* h = s_malloc0(sizeof(*h));
    h->runtime = JS_NewRuntime(8L*1024L*1024L);
    h->context = JS_NewContext(h->runtime, 16*1024);
    JS_SetOptions(h->context, 
	JSOPTION_VAROBJFIX|JSOPTION_COMPILE_N_GO|JSOPTION_NO_SCRIPT_RVAL);
    JS_SetErrorReporter(h->context, report_error);
#ifdef MOZJS_185
    h->global = JS_NewCompartmentAndGlobalObject(h->context, &global_class, NULL);
#else
    h->global = JS_NewGlobalObject(h->context,&global_class,NULL);
#endif
    JS_InitStandardClasses(h->context, h->global);
    return h;
}
Ejemplo n.º 26
0
static ctxStore *ctxs_new(pxPAC *pac)
{
	JSObject *global = NULL;
	jsval     rval;

	// Create the basic context
	ctxStore *self = px_malloc0(sizeof(ctxStore));

	// Setup Javascript global class
	self->cls     = px_malloc0(sizeof(JSClass));
	self->cls->name        = "global";
	self->cls->flags       = 0;
	self->cls->addProperty = JS_PropertyStub;
	self->cls->delProperty = JS_PropertyStub;
	self->cls->getProperty = JS_PropertyStub;
	self->cls->setProperty = JS_PropertyStub;
	self->cls->enumerate   = JS_EnumerateStub;
	self->cls->resolve     = JS_ResolveStub;
	self->cls->convert     = JS_ConvertStub;
	self->cls->finalize    = JS_FinalizeStub;

	// Initialize Javascript runtime environment
	if (!(self->run = JS_NewRuntime(1024 * 1024)))                   goto error;
	if (!(self->ctx = JS_NewContext(self->run, 1024 * 1024)))        goto error;
	if (!(global  = JS_NewObject(self->ctx, self->cls, NULL, NULL))) goto error;
	JS_InitStandardClasses(self->ctx, global);

	// Define Javascript functions
	JS_DefineFunction(self->ctx, global, "dnsResolve", dnsResolve, 1, 0);
	JS_DefineFunction(self->ctx, global, "myIpAddress", myIpAddress, 0, 0);
	JS_EvaluateScript(self->ctx, global, JAVASCRIPT_ROUTINES,
			        strlen(JAVASCRIPT_ROUTINES), "pacutils.js", 0, &rval);

	// Add PAC to the environment
	JS_EvaluateScript(self->ctx, global, px_pac_to_string(pac),
                       strlen(px_pac_to_string(pac)),
                       px_url_to_string((pxURL *) px_pac_get_url(pac)),
                       0, &rval);

	// Save the pac
	self->pac = px_strdup(px_pac_to_string(pac));
	return self;

error:
	ctxs_free(self);
	return NULL;
}
Ejemplo n.º 27
0
/*----------------------------------------------------------------------------*
 *                           Scripter descriptor                              *
 *----------------------------------------------------------------------------*/
static void * _egueb_script_js_sm_scripter_new(void)
{
	Egueb_Script_Js_Sm_Scripter *thiz;

	thiz = calloc(1, sizeof(Egueb_Script_Js_Sm_Scripter));
	/* Create an instance of the engine */
	thiz->rt = JS_NewRuntime(1024 * 1024);
	JS_SetRuntimePrivate(thiz->rt, thiz);

	/* Create an execution context */
	thiz->cx = JS_NewContext(thiz->rt, 8192);
	JS_SetOptions(thiz->cx, JS_GetOptions(thiz->cx) | JSOPTION_VAROBJFIX);
	JS_SetErrorReporter(thiz->cx, _egueb_script_js_sm_scripter_report_error);

	_egueb_script_js_sm_init_global_object(thiz);
	return thiz;
}
Ejemplo n.º 28
0
bool
JetpackChild::Init(base::ProcessHandle aParentProcessHandle,
                   MessageLoop* aIOLoop,
                   IPC::Channel* aChannel)
{
  if (!Open(aChannel, aParentProcessHandle, aIOLoop))
    return false;

  if (!(mRuntime = JS_NewRuntime(32L * 1024L * 1024L)) ||
      !(mCx = JS_NewContext(mRuntime, 8192)))
    return false;

  JS_SetVersion(mCx, JSVERSION_LATEST);
  JS_SetOptions(mCx, JS_GetOptions(mCx) |
                JSOPTION_DONT_REPORT_UNCAUGHT |
                JSOPTION_ATLINE |
                JSOPTION_JIT);
  JS_SetErrorReporter(mCx, ReportError);

  {
    JSAutoRequest request(mCx);
    JS_SetContextPrivate(mCx, this);
    JSObject* implGlobal =
      JS_NewCompartmentAndGlobalObject(mCx, const_cast<JSClass*>(&sGlobalClass), NULL);
    if (!implGlobal)
        return false;

    JSAutoEnterCompartment ac;
    if (!ac.enter(mCx, implGlobal))
        return false;

    jsval ctypes;
    if (!JS_InitStandardClasses(mCx, implGlobal) ||
#ifdef BUILD_CTYPES
        !JS_InitCTypesClass(mCx, implGlobal) ||
        !JS_GetProperty(mCx, implGlobal, "ctypes", &ctypes) ||
        !JS_SetCTypesCallbacks(mCx, JSVAL_TO_OBJECT(ctypes), &sCallbacks) ||
#endif
        !JS_DefineFunctions(mCx, implGlobal,
                            const_cast<JSFunctionSpec*>(sImplMethods)))
      return false;
  }

  return true;
}
Ejemplo n.º 29
0
    Application::Application() :
        _myTerminate(false), _myInterval(100000), _myDebugger(0) {

        JSRuntime *myRuntime = JS_NewRuntime(JS_HEAP_SIZE);

        JSContext *myContext = JS_NewContext(myRuntime, JS_STACK_CHUNK);
        JS_ToggleOptions(myContext, JSOPTION_STRICT);

        JSObject  *myGlobal  = JS_NewObject(myContext, &tuttle_global_class, NULL, NULL);

        JS_InitStandardClasses(myContext, myGlobal);

        JS_DefineFunctions(myContext, myGlobal, tuttle_global_functions);

        _myRuntime = myRuntime;
        _myContext = myContext;
        _myGlobal  = myGlobal;
    }
Ejemplo n.º 30
0
JSContext * InitializeSpiderMonkey() {

	runtime = JS_NewRuntime(jsRuntimeSize);
	JSContext * context = JS_NewContext(runtime, jsStackSize);

	JS_SetOptions(context,
					JSOPTION_VAROBJFIX |
					JSOPTION_JIT |
					JSOPTION_METHODJIT |
					JSOPTION_COMPILE_N_GO
					);

	JS_SetVersion(context, JSVERSION_LATEST);

	JS_SetErrorReporter(context, reportError);

	return context;

}