Esempio n. 1
0
JS_EvaluateUCInStackFrame(JSContext *cx, JSStackFrame *fp,
                          const jschar *bytes, uintN length,
                          const char *filename, uintN lineno,
                          jsval *rval)
{
    uint32 flags;
    JSScript *script;
    JSBool ok;

    /*
     * XXX Hack around ancient compiler API to propagate the JSFRAME_SPECIAL
     * flags to the code generator (see js_EmitTree's TOK_SEMI case).
     */
    flags = fp->flags;
    fp->flags |= JSFRAME_DEBUGGER | JSFRAME_EVAL;
    script = JS_CompileUCScriptForPrincipals(cx, fp->scopeChain,
                                             fp->script
                                             ? fp->script->principals
                                             : NULL,
                                             bytes, length, filename, lineno);
    fp->flags = flags;
    if (!script)
        return JS_FALSE;

    ok = js_Execute(cx, fp->scopeChain, script, fp,
                    JSFRAME_DEBUGGER | JSFRAME_EVAL, rval);
    js_DestroyScript(cx, script);
    return ok;
}
NS_IMETHODIMP
nsDOMWorkerScriptLoader::ScriptCompiler::Run()
{
  NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");

  if (mRevoked) {
    return NS_OK;
  }

  NS_ASSERTION(!mScriptObj.ToJSObject(), "Already have a script object?!");
  NS_ASSERTION(mScriptObj.IsHeld(), "Not held?!");
  NS_ASSERTION(!mScriptText.IsEmpty(), "Shouldn't have empty source here!");

  JSContext* cx = nsDOMThreadService::GetCurrentContext();
  NS_ENSURE_STATE(cx);

  JSAutoRequest ar(cx);

  JSObject* global = JS_GetGlobalObject(cx);
  NS_ENSURE_STATE(global);

  // Because we may have nested calls to this function we don't want the
  // execution to automatically report errors. We let them propagate instead.
  uint32 oldOpts =
    JS_SetOptions(cx, JS_GetOptions(cx) | JSOPTION_DONT_REPORT_UNCAUGHT);

  JSPrincipals* principal = nsDOMWorkerSecurityManager::WorkerPrincipal();

  JSScript* script =
    JS_CompileUCScriptForPrincipals(cx, global, principal,
                                    reinterpret_cast<const jschar*>
                                               (mScriptText.BeginReading()),
                                    mScriptText.Length(), mFilename.get(), 1);

  JS_SetOptions(cx, oldOpts);

  if (!script) {
    return NS_ERROR_FAILURE;
  }

  mScriptObj = JS_NewScriptObject(cx, script);
  NS_ENSURE_STATE(mScriptObj.ToJSObject());

  return NS_OK;
}
Esempio n. 3
0
static JSBool
obj_eval(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
    JSStackFrame *fp, *caller;
    JSBool ok;
    JSString *str;
    const char *file;
    uintN line;
    JSPrincipals *principals;
    JSScript *script;
#if JS_HAS_EVAL_THIS_SCOPE
    JSObject *callerScopeChain;
    JSBool implicitWith;
#endif

    if (!JSVAL_IS_STRING(argv[0])) {
	*rval = argv[0];
	return JS_TRUE;
    }

    fp = cx->fp;
    caller = fp->down;
#if !JS_BUG_EVAL_THIS_FUN
    /* Ensure that this flows into eval from the calling function, if any. */
    fp->thisp = caller->thisp;
#endif
#if JS_HAS_SHARP_VARS
    fp->sharpArray = caller->sharpArray;
#endif

#if JS_HAS_EVAL_THIS_SCOPE
    /* If obj.eval(str), emulate 'with (obj) eval(str)' in the calling frame. */
    callerScopeChain = caller->scopeChain;
    implicitWith = (callerScopeChain != obj &&
		    (callerScopeChain->map->clasp != &js_WithClass ||
		     OBJ_GET_PROTO(callerScopeChain) != obj));
    if (implicitWith) {
	obj = js_NewObject(cx, &js_WithClass, obj, callerScopeChain);
	if (!obj)
	    return JS_FALSE;
	caller->scopeChain = obj;
    }
#endif

#if !JS_BUG_EVAL_THIS_SCOPE
    /* Compile using caller's current scope object (might be a function). */
    obj = caller->scopeChain;
#endif

    str = JSVAL_TO_STRING(argv[0]);
    if (caller->script) {
	file = caller->script->filename;
	line = js_PCToLineNumber(caller->script, caller->pc);
	principals = caller->script->principals;
    } else {
	file = NULL;
	line = 0;
	principals = NULL;
    }
    script = JS_CompileUCScriptForPrincipals(cx, obj, principals,
					     str->chars, str->length,
					     file, line);
    if (!script) {
	ok = JS_FALSE;
	goto out;
    }

#if !JS_BUG_EVAL_THIS_SCOPE
    /* Interpret using caller's new scope object (might be a Call object). */
    obj = caller->scopeChain;
#endif
    ok = js_Execute(cx, obj, script, fp, rval);
    JS_DestroyScript(cx, script);

out:
#if JS_HAS_EVAL_THIS_SCOPE
    if (implicitWith) {
	/* Restore OBJ_GET_PARENT(obj) not callerScopeChain in case of Call. */
	caller->scopeChain = OBJ_GET_PARENT(obj);
    }
#endif
    return ok;
}