Ejemplo n.º 1
0
void DLLCALL js_EvalOnExit(JSContext *cx, JSObject *obj, js_callback_t* cb)
{
	char*	p;
	jsval	rval;
	JSObject* script;
	BOOL	auto_terminate=cb->auto_terminate;
	JSObject	*glob=JS_GetGlobalObject(cx);
	global_private_t *pt;
	str_list_t	list;

	if(glob==obj) {
		pt=(global_private_t *)JS_GetPrivate(cx,JS_GetGlobalObject(cx));		
		list=pt->exit_func;
	}
	else
		list=JS_GetPrivate(cx,obj);

	cb->auto_terminate=FALSE;

	while((p=strListPop(&list))!=NULL) {
		if((script=JS_CompileScript(cx, obj, p, strlen(p), NULL, 0))!=NULL) {
			JS_ExecuteScript(cx, obj, script, &rval);
		}
		free(p);
	}

	strListFree(&list);
	if(glob != obj)
		JS_SetPrivate(cx,obj,NULL);
	else
		pt->exit_func=NULL;

	if(auto_terminate)
		cb->auto_terminate = TRUE;
}
Ejemplo n.º 2
0
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScriptForPrincipals)
{
    JS::CompileOptions options(cx);
    options.setFileAndLine(__FILE__, __LINE__);
    return tryScript(global, JS_CompileScript(cx, global, code, code_size,
                                              options));
}
Ejemplo n.º 3
0
// compile a script and root it to an object
int JsParser::use(JSContext *cx, JSObject *obj, const char* script_file) {
  func("%u:%s:%s",__LINE__,__FILE__,__FUNCTION__);
  JSObject *scriptObject;
  JSScript *script;
  FILE *fd;
  char *buf = NULL;
  int len;

  fd = fopen(script_file,"r");
  if(!fd) {
    JS_ReportErrorNumber(
      cx, JSFreej_GetErrorMessage, NULL,
      JSSMSG_FJ_WICKED, script_file, strerror(errno)
    );
    return 0;
  }
  buf = readFile(fd, &len);
  fclose(fd);

  if (!buf) {
    JS_ReportErrorNumber(
      cx, JSFreej_GetErrorMessage, NULL,
      JSSMSG_FJ_WICKED, script_file, "No buffer for file .... out of memory?"
    );
    return 0;
  }
  JS_BeginRequest(cx);

  /* TODO -- FIX
  // use a clean obj and put freej inside
	scriptObject = JS_NewObject(cx, &UseScriptClass, NULL, NULL);
	init_class(cx, scriptObject);
  */
  notice("%s from: %p new: %p glob: %p", __PRETTY_FUNCTION__, obj, scriptObject, global_object);
  if(!scriptObject){
    JS_ReportError(cx, "Can't create script");
    return JS_FALSE;
  }

  script = JS_CompileScript(cx, scriptObject, buf, len, script_file, 0);
  if(!script){
    JS_ReportError(cx, "Can't compile script");
    return JS_FALSE;
  }

  jsval rval;
  JS_ExecuteScriptPart(cx, scriptObject, script, JSEXEC_PROLOG, &rval);

  /* save script as private data for the object */
  if(!JS_SetPrivate(cx, scriptObject, (void*)script)){
    JS_EndRequest(cx);
    return JS_FALSE;
  }

  JS_DefineFunction(cx, scriptObject, "exec", ExecScript, 0, 0);
  JS_EndRequest(cx);
  return OBJECT_TO_JSVAL(scriptObject);
}
Ejemplo n.º 4
0
/* Execute a string in its own context (away from Synchronet objects) */
static JSBool
js_eval(JSContext *parent_cx, uintN argc, jsval *arglist)
{
	jsval *argv=JS_ARGV(parent_cx, arglist);
	char*			buf;
	size_t			buflen;
	JSString*		str;
    JSObject*		script;
	JSContext*		cx;
	JSObject*		obj;
	JSErrorReporter	reporter;

	JS_SET_RVAL(cx, arglist, JSVAL_VOID);

	if(argc<1)
		return(JS_TRUE);

	if((str=JS_ValueToString(parent_cx, argv[0]))==NULL)
		return(JS_FALSE);
	JSSTRING_TO_MSTRING(parent_cx, str, buf, &buflen);
	HANDLE_PENDING(parent_cx);
	if(buf==NULL)
		return(JS_TRUE);

	if((cx=JS_NewContext(JS_GetRuntime(parent_cx),JAVASCRIPT_CONTEXT_STACK))==NULL) {
		free(buf);
		return(JS_FALSE);
	}

	/* Use the error reporter from the parent context */
	reporter=JS_SetErrorReporter(parent_cx,NULL);
	JS_SetErrorReporter(parent_cx,reporter);
	JS_SetErrorReporter(cx,reporter);

	/* Use the operation callback from the parent context */
	JS_SetContextPrivate(cx, JS_GetContextPrivate(parent_cx));
	JS_SetOperationCallback(cx, JS_GetOperationCallback(parent_cx));

	if((obj=JS_NewCompartmentAndGlobalObject(cx, &eval_class, NULL))==NULL
		|| !JS_InitStandardClasses(cx,obj)) {
		JS_DestroyContext(cx);
		free(buf);
		return(JS_FALSE);
	}

	if((script=JS_CompileScript(cx, obj, buf, buflen, NULL, 0))!=NULL) {
		jsval	rval;

		JS_ExecuteScript(cx, obj, script, &rval);
		JS_SET_RVAL(cx, arglist, rval);
	}
	free(buf);

	JS_DestroyContext(cx);

    return(JS_TRUE);
}
Ejemplo n.º 5
0
/* Execute a string in its own context (away from Synchronet objects) */
static JSBool
js_eval(JSContext *parent_cx, JSObject *parent_obj, uintN argc, jsval *argv, jsval *rval)
{
    char*			buf;
    size_t			buflen;
    JSString*		str;
    JSScript*		script;
    JSContext*		cx;
    JSObject*		obj;
    JSErrorReporter	reporter;
#ifndef EVAL_BRANCH_CALLBACK
    JSBranchCallback callback;
#endif

    if(argc<1)
        return(JS_TRUE);

    if((str=JS_ValueToString(parent_cx, argv[0]))==NULL)
        return(JS_FALSE);
    if((buf=JS_GetStringBytes(str))==NULL)
        return(JS_FALSE);
    buflen=JS_GetStringLength(str);

    if((cx=JS_NewContext(JS_GetRuntime(parent_cx),JAVASCRIPT_CONTEXT_STACK))==NULL)
        return(JS_FALSE);

    /* Use the error reporter from the parent context */
    reporter=JS_SetErrorReporter(parent_cx,NULL);
    JS_SetErrorReporter(parent_cx,reporter);
    JS_SetErrorReporter(cx,reporter);

#ifdef EVAL_BRANCH_CALLBACK
    JS_SetContextPrivate(cx, JS_GetPrivate(parent_cx, parent_obj));
    JS_SetBranchCallback(cx, js_BranchCallback);
#else	/* Use the branch callback from the parent context */
    JS_SetContextPrivate(cx, JS_GetContextPrivate(parent_cx));
    callback=JS_SetBranchCallback(parent_cx,NULL);
    JS_SetBranchCallback(parent_cx, callback);
    JS_SetBranchCallback(cx, callback);
#endif

    if((obj=JS_NewObject(cx, NULL, NULL, NULL))==NULL
            || !JS_InitStandardClasses(cx,obj)) {
        JS_DestroyContext(cx);
        return(JS_FALSE);
    }

    if((script=JS_CompileScript(cx, obj, buf, buflen, NULL, 0))!=NULL) {
        JS_ExecuteScript(cx, obj, script, rval);
        JS_DestroyScript(cx, script);
    }

    JS_DestroyContext(cx);

    return(JS_TRUE);
}
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;
}
Ejemplo n.º 7
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;
}
JSBool ScriptingCore::evalString(const char *string, jsval *outVal, const char *filename)
{
    jsval rval;
    JSScript* script = JS_CompileScript(cx, global, string, strlen(string), filename, 1);
    if (script) {
        filename_script[filename] = script;
        JSBool evaluatedOK = JS_ExecuteScript(_cx, global, script, &rval);
        if (JS_FALSE == evaluatedOK) {
            LOGD(stderr, "(evaluatedOK == JS_FALSE)");
        }
        return evaluatedOK;
    }
    return false;
}
Ejemplo n.º 9
0
void DLLCALL js_EvalOnExit(JSContext *cx, JSObject *obj, js_branch_t* branch)
{
    char*	p;
    jsval	rval;
    JSScript* script;

    while((p=strListPop(&branch->exit_func))!=NULL) {
        if((script=JS_CompileScript(cx, obj, p, strlen(p), NULL, 0))!=NULL) {
            JS_ExecuteScript(cx, obj, script, &rval);
            JS_DestroyScript(cx, script);
        }
    }

    strListFree(&branch->exit_func);
}
Ejemplo n.º 10
0
nsresult
ProxyAutoConfig::SetupJS()
{
  mJSNeedsSetup = false;
  NS_ABORT_IF_FALSE(!sRunning, "JIT is running");

  delete mJSRuntime;
  mJSRuntime = nullptr;

  if (mPACScript.IsEmpty())
    return NS_ERROR_FAILURE;

  mJSRuntime = JSRuntimeWrapper::Create();
  if (!mJSRuntime)
    return NS_ERROR_FAILURE;

  JSAutoRequest ar(mJSRuntime->Context());
  JSAutoCompartment ac(mJSRuntime->Context(), mJSRuntime->Global());

  sRunning = this;
  JSScript *script = JS_CompileScript(mJSRuntime->Context(),
                                      mJSRuntime->Global(),
                                      mPACScript.get(), mPACScript.Length(),
                                      mPACURI.get(), 1);
  if (!script ||
      !JS_ExecuteScript(mJSRuntime->Context(), mJSRuntime->Global(), script, nullptr)) {
    nsString alertMessage(NS_LITERAL_STRING("PAC file failed to install from "));
    alertMessage += NS_ConvertUTF8toUTF16(mPACURI);
    PACLogToConsole(alertMessage);
    sRunning = nullptr;
    return NS_ERROR_FAILURE;
  }
  sRunning = nullptr;

  mJSRuntime->SetOK();
  nsString alertMessage(NS_LITERAL_STRING("PAC file installed from "));
  alertMessage += NS_ConvertUTF8toUTF16(mPACURI);
  PACLogToConsole(alertMessage);

  // we don't need these now
  mPACScript.Truncate();
  mPACURI.Truncate();

  return NS_OK;
}
Ejemplo n.º 11
0
static void ctx_compile(INT32 args)
{
  JSScript           *compiled;
  struct pike_string *script;
  jsval               rval;
  INT32               version = -1, oldversion = -1;

  if (!THIS->ctx) {
    pop_n_elems(args);
    push_int(0);
    return;
  }
    
  switch(args) {
      case 2:
        get_all_args("compile", args, "%S%i", &script, &version);
        break;

      case 1:
        get_all_args("compile", args, "%S", &script);
        break;

      default:
        Pike_error("Not enough arguments\n");
  }

  if (version != -1)
    oldversion = JS_SetVersion(THIS->ctx, version);
    
  /* TODO: filename should indicate the actual location of the script */
  compiled = JS_CompileScript(THIS->ctx, global,
                               script->str, script->len,
                              "Caudium/js", &rval);

  if (oldversion != -1)
    JS_SetVersion(THIS->ctx, oldversion);
    
  pop_n_elems(args);
    
  if (!compiled) {
    push_int(-1);
    return;
  }
}
Ejemplo n.º 12
0
JSBool ScriptingCore::evalString(const char *string, jsval *outVal, const char *filename, JSContext* cx, JSObject* global)
{
    jsval rval;
    if (cx == NULL)
        cx = cx_;
    if (global == NULL)
        global = global_;
    JSScript* script = JS_CompileScript(cx, global, string, strlen(string), filename, 1);
    if (script) {
        // JSAutoCompartment ac(cx, global);
		JSAutoCompartment ac(cx, global);
        JSBool evaluatedOK = JS_ExecuteScript(cx, global, script, &rval);
        if (JS_FALSE == evaluatedOK) {
            fprintf(stderr, "(evaluatedOK == JS_FALSE)\n");
        }
        return evaluatedOK;
    }
    return false;
}
Ejemplo n.º 13
0
static Eina_Bool _egueb_script_js_sm_scripter_load(void *prv, Egueb_Dom_String *s, void **obj)
{
	Egueb_Script_Js_Sm_Scripter *thiz = prv;
	Egueb_Dom_String *uri = NULL;
	jsval val;
	JSObject *so;
	Eina_Bool ret = EINA_FALSE;
	const char *data;

	*obj = NULL;
	data = egueb_dom_string_chars_get(s);
	/* get the document, get the uri of the document, pass it as the filename */
	if (JS_GetProperty(thiz->cx, thiz->global, "document", &val))
	{
		JSObject *doc;

		doc = JSVAL_TO_OBJECT(val);
		if (ender_js_sm_is_instance(thiz->cx, doc))
		{
			Egueb_Dom_Node *n;
			n = ender_js_sm_instance_ptr_get(thiz->cx, doc);
			uri = egueb_dom_document_uri_get(n);
		}
	}
	so = JS_CompileScript(thiz->cx, NULL, data, strlen(data),
			uri ? egueb_dom_string_chars_get(uri) : NULL, 1);
	if (so)
	{
		Egueb_Script_Js_Sm_Scripter_Script *script;
		script = calloc(1, sizeof(Egueb_Script_Js_Sm_Scripter_Script));
		script->obj = so;
		*obj = script;
		ret = EINA_TRUE;
	}
	if (uri)
	{
		egueb_dom_string_unref(uri);
	}

	return ret;
}
Ejemplo n.º 14
0
void JavaScriptEngine::RunScript(const char *fileName) {
	char * scriptStr = LoadJsFile(fileName);
	if (scriptStr == NULL) {
		return;
	}
	int lineNumber = 0;
	JSObject *script = JS_CompileScript(globalContext, globalObject, scriptStr,
			strlen(scriptStr), fileName, lineNumber);
	delete[] scriptStr;
	if (script == NULL) {
		return;
	}
	jsval rval;
	JSBool status =
			JS_ExecuteScript(globalContext, globalObject, script, &rval);
	if (status == JS_FALSE) {
		if (JSVAL_IS_NULL(rval)) {
			LogInfo(TAG, "rval : (JSVAL_IS_NULL(rval)");
		} else if ((JSVAL_IS_BOOLEAN(rval)) && (JS_FALSE == (JSVAL_TO_BOOLEAN(
				rval)))) {
			LogInfo(TAG, "rval : (return value is JS_FALSE");
		} else if (JSVAL_IS_STRING(rval)) {
			JSString *str = JS_ValueToString(globalContext, rval);
			if (str == NULL) {
				LogInfo(TAG, "rval : return string is NULL");
			} else {
				char * content = JS_EncodeString(globalContext, str);
				LogInfo(TAG, "rval : return string =\n%s\n", content);
				JS_free(globalContext, content);
			}
		} else if (JSVAL_IS_NUMBER(rval)) {
			double number;
			if (JS_FALSE == JS_ValueToNumber(globalContext, rval, &number)) {
				LogInfo(TAG, "rval : return number could not be converted");
			} else {
				LogInfo(TAG, "rval : return number =\n%f", number);
			}
		}
	}
}
Ejemplo n.º 15
0
/*
 * call-seq:
 *   native_compile(script, filename, linenum)
 *
 * Compile +script+ with +filename+ using +linenum+
 */
static VALUE native_compile(VALUE self, VALUE script, VALUE filename, VALUE linenum)
{
  JohnsonRuntime* runtime;
  Data_Get_Struct(self, JohnsonRuntime, runtime);

  JSContext * context = johnson_get_current_context(runtime);
  JohnsonContext * johnson_context = OUR_CONTEXT(context);

  JSScript * compiled_js = JS_CompileScript(
      context,
      runtime->global,
      StringValuePtr(script),
      (size_t)StringValueLen(script),
      StringValueCStr(filename),
      (unsigned)NUM2INT(linenum)
  );
  if(compiled_js == NULL) {
    if (JS_IsExceptionPending(context))
    {
      // If there's an exception pending here, it's a syntax error.
      JS_GetPendingException(context, &johnson_context->ex);
      JS_ClearPendingException(context);
    }

    if (johnson_context->ex) {
      RAISE_JS_ERROR(self, johnson_context->ex);
      return Qnil;
    }
  }

  JSObject * script_object = JS_NewScriptObject(context, compiled_js);

  PREPARE_RUBY_JROOTS(context, 1);
  JROOT(script_object);
  JRETURN_RUBY(make_ruby_land_proxy(runtime, OBJECT_TO_JSVAL(script_object), "JSScriptProxy"));
}
Ejemplo n.º 16
0
nsresult
ProxyAutoConfig::SetupJS()
{
  mJSNeedsSetup = false;
  NS_ABORT_IF_FALSE(!sRunning, "JIT is running");

  delete mJSRuntime;
  mJSRuntime = nullptr;

  if (mPACScript.IsEmpty())
    return NS_ERROR_FAILURE;

  mJSRuntime = JSRuntimeWrapper::Create();
  if (!mJSRuntime)
    return NS_ERROR_FAILURE;

  JSAutoRequest ar(mJSRuntime->Context());
  JSAutoCompartment ac(mJSRuntime->Context(), mJSRuntime->Global());

  // check if this is a data: uri so that we don't spam the js console with
  // huge meaningless strings. this is not on the main thread, so it can't
  // use nsIRUI scheme methods
  bool isDataURI = nsDependentCSubstring(mPACURI, 0, 5).LowerCaseEqualsASCII("data:", 5);

  sRunning = this;
  JS::Rooted<JSObject *> global(mJSRuntime->Context(), mJSRuntime->Global());
  JS::CompileOptions options(mJSRuntime->Context());
  options.setFileAndLine(mPACURI.get(), 1);
  JSScript *script = JS_CompileScript(mJSRuntime->Context(), global,
                                      mPACScript.get(), mPACScript.Length(),
                                      options);
  if (!script ||
      !JS_ExecuteScript(mJSRuntime->Context(), mJSRuntime->Global(), script, nullptr)) {
    nsString alertMessage(NS_LITERAL_STRING("PAC file failed to install from "));
    if (isDataURI) {
      alertMessage += NS_LITERAL_STRING("data: URI");
    }
    else {
      alertMessage += NS_ConvertUTF8toUTF16(mPACURI);
    }
    PACLogToConsole(alertMessage);
    sRunning = nullptr;
    return NS_ERROR_FAILURE;
  }
  sRunning = nullptr;

  mJSRuntime->SetOK();
  nsString alertMessage(NS_LITERAL_STRING("PAC file installed from "));
  if (isDataURI) {
    alertMessage += NS_LITERAL_STRING("data: URI");
  }
  else {
    alertMessage += NS_ConvertUTF8toUTF16(mPACURI);
  }
  PACLogToConsole(alertMessage);

  // we don't need these now
  mPACScript.Truncate();
  mPACURI.Truncate();

  return NS_OK;
}
Ejemplo n.º 17
0
int
js_plugin_load(const char *id, const char *url, char *errbuf, size_t errlen)
{
  char *sbuf;
  struct fa_stat fs;
  JSContext *cx;
  js_plugin_t *jsp;
  JSObject *pobj, *gobj, *confobj;
  JSScript *s;
  char path[PATH_MAX];
  jsval val;

  if((sbuf = fa_quickload(url, &fs, NULL, errbuf, errlen)) == NULL)
    return -1;

  cx = js_newctx(err_reporter);
  JS_BeginRequest(cx);

  /* Remove any plugin with same URL */
  LIST_FOREACH(jsp, &js_plugins, jsp_link)
    if(!strcmp(jsp->jsp_id, id))
      break;
  if(jsp != NULL)
    js_plugin_unload(cx, jsp);

  jsp = calloc(1, sizeof(js_plugin_t));
  jsp->jsp_url = strdup(url);
  jsp->jsp_id  = strdup(id);

  LIST_INSERT_HEAD(&js_plugins, jsp, jsp_link);

  gobj = JS_NewObject(cx, &global_class, NULL, NULL);
  JS_InitStandardClasses(cx, gobj);

  JS_DefineProperty(cx, gobj, "showtime", OBJECT_TO_JSVAL(showtimeobj),
		    NULL, NULL, JSPROP_READONLY | JSPROP_PERMANENT);

  /* Plugin object */
  pobj = JS_NewObject(cx, &plugin_class, NULL, gobj);
  JS_AddNamedRoot(cx, &pobj, "plugin");

  JS_SetPrivate(cx, pobj, jsp);

  JS_DefineFunctions(cx, pobj, plugin_functions);

  /* Plugin config object */
  confobj = JS_DefineObject(cx, pobj, "config", &plugin_conf_class, NULL, 0);

  JS_SetPrivate(cx, confobj, jsp);

  val = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, url));
  JS_SetProperty(cx, confobj, "url", &val);

  if(!fa_parent(path, sizeof(path), url)) {
    val = STRING_TO_JSVAL(JS_NewStringCopyZ(cx, path));
    JS_SetProperty(cx, confobj, "path", &val);
  }

  JS_DefineProperty(cx, confobj, "URIRouting", BOOLEAN_TO_JSVAL(1),
		    NULL, jsp_setEnableURIRoute, JSPROP_PERMANENT);

  JS_DefineProperty(cx, confobj, "search", BOOLEAN_TO_JSVAL(1),
		    NULL, jsp_setEnableSearch, JSPROP_PERMANENT);

  s = JS_CompileScript(cx, pobj, sbuf, fs.fs_size, url, 1);
  free(sbuf);

  if(s != NULL) {
    JSObject *sobj = JS_NewScriptObject(cx, s);
    jsval result;

    JS_AddNamedRoot(cx, &sobj, "script");
    JS_ExecuteScript(cx, pobj, s, &result);
    JS_RemoveRoot(cx, &sobj);
  }

  JS_RemoveRoot(cx, &pobj);
  JS_EndRequest(cx);
  JS_GC(cx);
  JS_DestroyContext(cx);
  return 0;
}
Ejemplo n.º 18
0
BEGIN_FIXTURE_TEST(ScriptObjectFixture, bug438633_CompileScript_empty)
{
    JS::CompileOptions options(cx);
    options.setFileAndLine(__FILE__, __LINE__);
    return tryScript(global, JS_CompileScript(cx, global, "", 0, options));
}
Ejemplo n.º 19
0
/*
 * Purpose: Carry out rule running in this spun off thread
 *
 * Entry:
 * 	1st - Queue entry
 *
 * Exit:
 *	SUCCESS = ERR_NONE
 * 	FAILURE = ERR_* (type of error)
*/
ERRTYPE spawnRuleRunner(Queue_Entry *qentry) {
	JSObject *script = NULL;
	JSRuntime *rt = NULL;
	JSContext *cx = NULL;
	JSBool ret;
	JSObject *global;
	jsval rval;
	Logic_Entry *lentry;

	if((lentry = getLogicEntryForVoid(qentry->voidId)) == NULL) {
		printf("logic 2\r\n");
		return ERR_NONE;
	}

	rt = JS_NewRuntime(SPIDERMONKEY_ALLOC_RAM);

	if(rt == NULL) {
		return ERR_UNKNOWN;
	}

	/*
	 * 8192 = size of each stack chunk (not stack size)
	 *
	 * Apparently this is an internal variable in spidermonkey
	 * that shouldn't be tweaked without knowing a lot about
	 * spidermonkey's garbage collection.
	*/
	cx = JS_NewContext(rt, 8192);

	if(cx == NULL) {
		JS_DestroyRuntime(rt);
		return ERR_UNKNOWN;
	}

	JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_COMPILE_N_GO); // JSOPTION_METHODJIT
	JS_SetVersion(cx, JSVERSION_LATEST);

	JS_SetErrorReporter(cx, jsErrorHandler);

	// Create the global object in a new compartment. See http://developer.mozilla.org/En/SpiderMonkey/JSAPI_User_Guide#Native_functions
	global = JS_NewCompartmentAndGlobalObject(cx, &js_global_object_class, NULL);

    	if (global == NULL) {
		JS_DestroyContext(cx);
		JS_DestroyRuntime(rt);
		return ERR_UNKNOWN;
	}

	if(JS_InitStandardClasses(cx, global) == false) {
		JS_DestroyContext(cx);
		JS_DestroyRuntime(rt);
		return ERR_UNKNOWN;
	}

	createJSObjectThwonk(cx, global, qentry);

	script = JS_CompileScript(cx, global, lentry->logic, strlen(lentry->logic), "<inline>", 0);

	if(script == NULL) {
		// TODO: Log error to database for script writer to see
		printf("Couldn't compiled the script\n");
		JS_DestroyContext(cx);
		JS_DestroyRuntime(rt);
		return ERR_UNKNOWN;
	}

	ret = JS_ExecuteScript(cx, global, script, &rval);
 
	if(ret == JS_FALSE) {
		// TODO: Log error to database for script writer to see
		printf("Failed to run compiled script.\n");
		JS_DestroyContext(cx);
		JS_DestroyRuntime(rt);
		return ERR_UNKNOWN;
	}

//	str = JS_ValueToString(cx, rval);

//	printf("script result: %s\n", JS_GetStringBytes(str));

	JS_DestroyContext(cx);
	JS_DestroyRuntime(rt);
	JS_ShutDown();		// Is this needed since thread is ending on return from this function?

	return ERR_NONE;
}
Ejemplo n.º 20
0
static void
Process(JSContext *cx, JSObject *obj, char *filename, JSBool forceTTY)
{
	SG_context * pCtx = NULL;
	char * sz = NULL;
	
    JSBool ok, hitEOF;
    JSObject *script;
    jsval result;
    JSString *str;
    char buffer[4096];
    char *bufp;
    int lineno;
    int startline;

    SetContextOptions(cx);

    if (!forceTTY && filename != NULL && strlen(filename) != 0) {
        script = JS_CompileFile(cx, obj, filename);
        if (script) {
            if (!compileOnly)
                (void)JS_ExecuteScript(cx, obj, script, &result);
        }

        return;
    }

	(void)SG_context__alloc(&pCtx);

    /* It's an interactive filehandle; drop into read-eval-print loop. */
    lineno = 1;
    hitEOF = JS_FALSE;
    do {
        bufp = buffer;
        *bufp = '\0';

        /*
         * Accumulate lines until we get a 'compilable unit' - one that either
         * generates an error (before running out of source) or that compiles
         * cleanly.  This should be whenever we get a complete statement that
         * coincides with the end of a line.
         */
        startline = lineno;
        do {
            if (!GetLine(pCtx, cx, bufp, (SG_uint32)(sizeof(buffer)-(bufp-buffer)), stdin, startline == lineno ? "vscript> " : "")) {
                hitEOF = JS_TRUE;
                break;
            }
            bufp += strlen(bufp);
            lineno++;
        } while (!JS_BufferIsCompilableUnit(cx, obj, buffer, strlen(buffer)));

        /* Clear any pending exception from previous failed compiles.  */
        JS_ClearPendingException(cx);
        script = JS_CompileScript(cx, obj, buffer, strlen(buffer), "typein",
                                  startline);
        if (script) {
            if (!compileOnly) {
                ok = JS_ExecuteScript(cx, obj, script, &result);
                if (ok && !JSVAL_IS_VOID(result)) {
                    str = JS_ValueToString(cx, result);
                    if (str)
                    {
                    	SG_ERR_IGNORE(  sg_jsglue__jsstring_to_sz(pCtx, cx, str, &sz)  );
                        fprintf(gOutFile, "%s\n", sz);
                        SG_NULLFREE(pCtx, sz);
                    }
                }
            }
        }
    } while (!hitEOF && !gQuitting);
    SG_CONTEXT_NULLFREE(pCtx);
    fprintf(gOutFile, "\n");
    return;
}