Example #1
0
void sm_stop(spidermonkey_vm *vm) {
  begin_request(vm);
  spidermonkey_state *state = (spidermonkey_state *) JS_GetContextPrivate(vm->context);
  state->terminate = 1;
  JS_SetContextPrivate(vm->context, state);

  //Wait for any executing function to stop
  //before beginning to free up any memory.
  while (JS_IsRunning(vm->context))  {
      sleep(1);
  }

  end_request(vm);

  //Now we should be free to proceed with
  //freeing up memory without worrying about
  //crashing the VM.
  if (state != NULL) {
    if (state->error != NULL) {
      free_error(state);
    }
    driver_free(state);
  }
  JS_SetContextPrivate(vm->context, NULL);
  JS_DestroyContext(vm->context);
  JS_DestroyRuntime(vm->runtime);
  driver_free(vm);
}
Example #2
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;
}
char *sm_eval(jaegermonkey_vm *vm, const char *filename, const char *code, int handle_retval) {
  char *retval = NULL;
  JSScript *script;
  jsval result;

  begin_request(vm);
  script = JS_CompileScript(vm->context,
			    vm->global,
			    code, strlen(code),
			    filename, 1);
  jaegermonkey_error *error = (jaegermonkey_error *) JS_GetContextPrivate(vm->context);
  if (error == NULL) {
    JS_ClearPendingException(vm->context);
    JS_ExecuteScript(vm->context, vm->global, script, &result);
    vm->invoke_count++;
    error = (jaegermonkey_error *) JS_GetContextPrivate(vm->context);
    if (error == NULL) {
      if (handle_retval) {
	if (JSVAL_IS_STRING(result)) {
	  JSString *str = JS_ValueToString(vm->context, result);
	  retval = copy_jsstring(str);
	}
	else if(strcmp(JS_GetStringBytes(JS_ValueToString(vm->context, result)), "undefined") == 0) {
	  retval = copy_string("{\"error\": \"Expression returned undefined\", \"lineno\": 0, \"source\": \"unknown\"}");
	}
	else {
	  retval = copy_string("{\"error\": \"non-JSON return value\", \"lineno\": 0, \"source\": \"unknown\"}");
	}
      }
      JS_DestroyScript(vm->context, script);
    }
    else {
      retval = error_to_json(error);
      free_error(error);
      JS_SetContextPrivate(vm->context, NULL);
    }
  }
  else {
    retval = error_to_json(error);
    free_error(error);
    JS_SetContextPrivate(vm->context, NULL);
  }
  if (vm->invoke_count > 200) {
    JS_GC(vm->context);
    vm->invoke_count = 0;
  }
  else {
    JS_MaybeGC(vm->context);
  }

  end_request(vm);
  return retval;
}
Example #4
0
char *sm_eval(spidermonkey_vm *vm, const char *filename, const char *code, int handle_retval) {
  char *retval = NULL;
  JSObject *script;
  jsval result;

  if (code == NULL) {
      return NULL;
  }

  begin_request(vm);
  script = JS_CompileScript(vm->context,
                            vm->global,
                            code, strlen(code),
                            filename, 1);
  spidermonkey_state *state = (spidermonkey_state *) JS_GetContextPrivate(vm->context);
  if (state->error == NULL) {
    JS_ClearPendingException(vm->context);
    JS_ExecuteScript(vm->context, vm->global, script, &result);
    state = (spidermonkey_state *) JS_GetContextPrivate(vm->context);
    if (state->error == NULL) {
      if (handle_retval) {
        if (JSVAL_IS_STRING(result)) {
          JSString *str = JS_ValueToString(vm->context, result);
          retval = copy_jsstring(vm->context, str);
        }
        else {
          char *tmp = JS_EncodeString(vm->context, JS_ValueToString(vm->context, result));
	  if(strcmp(tmp, "undefined") == 0) {
            retval = copy_string("{\"error\": \"Expression returned undefined\", \"lineno\": 0, \"source\": \"unknown\"}");
	  }
	  else {
            retval = copy_string("{\"error\": \"non-JSON return value\", \"lineno\": 0, \"source\": \"unknown\"}");
	  }
	  JS_free(vm->context, tmp);
        }
      }
    }
    else {
      retval = error_to_json(state->error);
      free_error(state);
      JS_SetContextPrivate(vm->context, state);
    }
  }
  else {
    retval = error_to_json(state->error);
    free_error(state);
    JS_SetContextPrivate(vm->context, state);
  }
  end_request(vm);
  return retval;
}
Example #5
0
spidermonkey_vm *sm_initialize(long heap_size) {
  spidermonkey_vm *vm = (spidermonkey_vm*) driver_alloc(sizeof(spidermonkey_vm));
  vm->runtime = JS_NewRuntime(MAX_GC_SIZE);
  JS_SetGCParameter(vm->runtime, JSGC_MAX_BYTES, heap_size);
  JS_SetGCParameter(vm->runtime, JSGC_MAX_MALLOC_BYTES, LAST_DITCH_GC_THRESHOLD);
  vm->context = JS_NewContext(vm->runtime, CONTEXT_THREAD_STACK_SIZE);
  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_NewObject(vm->context, &global_class, NULL, NULL);
  JS_InitStandardClasses(vm->context, vm->global);
  JS_SetErrorReporter(vm->context, on_error);
  JSNative *funptr = (JSNative *) *js_log;
  JS_DefineFunction(vm->context, JS_GetGlobalObject(vm->context), "ejsLog", funptr,
		    0, JSFUN_FAST_NATIVE);
  end_request(vm);
  return vm;
}
jaegermonkey_vm *sm_initialize(long thread_stack, long heap_size) {
  jaegermonkey_vm *vm = (jaegermonkey_vm*) driver_alloc(sizeof(jaegermonkey_vm));
  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_NewGlobalObject(vm->context, &global_class);
  JS_InitStandardClasses(vm->context, vm->global);
  JS_SetErrorReporter(vm->context, on_error);
  JS_DefineFunction(vm->context, JS_GetGlobalObject(vm->context), "ejsLog", js_log, 2, 0);
  end_request(vm);
  vm->invoke_count = 0;
  return vm;
}