@Override public Object call(ExecutionContext context) { Object self = context.getThisBinding(); Arguments argsObj = (Arguments) context.resolve("arguments").getValue(context); int numArgs = (int) argsObj.get(context, "length"); int paramsLen = getFormalParameters().length; Object[] args = new Object[numArgs < paramsLen ? paramsLen : numArgs]; for (int i = 0; i < numArgs; ++i) { Object v = argsObj.get(context, "" + i); if (v instanceof Reference) { if (((Reference) v).isUnresolvableReference()) { v = Types.UNDEFINED; } else { v = ((Reference) v).getValue(context); } } args[i] = v; } for (int i = numArgs; i < paramsLen; ++i) { args[i] = Types.UNDEFINED; } return call(context, self, args); }
public ExecutionContext createFunctionExecutionContext( Object functionReference, JSFunction function, Object thisArg, Object... arguments) { // 10.4.3 Object thisBinding = null; if (function.isStrict()) { thisBinding = thisArg; } else { if (thisArg == null || thisArg == Types.NULL || thisArg == Types.UNDEFINED) { thisBinding = this.getLexicalEnvironment().getGlobalObject(); } else if (!(thisArg instanceof JSObject)) { // thisBinding = Types.toObject(this, thisArg); thisBinding = Types.toThisObject(this, thisArg); } else { thisBinding = thisArg; } } LexicalEnvironment scope = function.getScope(); LexicalEnvironment localEnv = LexicalEnvironment.newDeclarativeEnvironment(scope); ExecutionContext context = new ExecutionContext(this, localEnv, localEnv, thisBinding, function.isStrict()); context.performDeclarationBindingInstantiation(function, arguments); context.fileName = function.getFileName(); // System.err.println( "debug null: " + ( function.getDebugContext() == null ? function : "not // null") ); context.debugContext = function.getDebugContext(); context.functionReference = functionReference; return context; }
public ExecutionContext createEvalExecutionContext(JSProgram eval, boolean direct) { // 10.4.2 (with caller) ExecutionContext context = null; Object evalThisBinding = null; LexicalEnvironment evalLexEnv = null; LexicalEnvironment evalVarEnv = null; if (!direct) { evalThisBinding = getGlobalObject(); evalLexEnv = LexicalEnvironment.newGlobalEnvironment(getGlobalObject()); evalVarEnv = LexicalEnvironment.newGlobalEnvironment(getGlobalObject()); } else { evalThisBinding = this.thisBinding; evalLexEnv = this.getLexicalEnvironment(); evalVarEnv = this.getVariableEnvironment(); } if (eval.isStrict()) { LexicalEnvironment strictVarEnv = LexicalEnvironment.newDeclarativeEnvironment(this.getLexicalEnvironment()); evalLexEnv = strictVarEnv; evalVarEnv = strictVarEnv; } context = new ExecutionContext(this, evalLexEnv, evalVarEnv, evalThisBinding, eval.isStrict()); context.performFunctionDeclarationBindings(eval, true); context.performVariableDeclarationBindings(eval, true); context.fileName = eval.getFileName(); return context; }
public FunctionDescriptor parseFunction(ExecutionContext context, String code) throws RecognitionException { final ANTLRStringStream stream = new ANTLRStringStream(code); ES3Lexer lexer = new ES3Lexer(stream); CommonTokenStream lexerStream = new CommonTokenStream(lexer); ES3Parser parser = new ES3Parser(lexerStream); ES3Parser.functionExpression_return function = parser.functionExpression(); List<String> errors = parser.getErrors(); if (!errors.isEmpty()) { throw new ThrowException(context, context.createSyntaxError(errors.get(0))); } CommonTree tree = (CommonTree) function.getTree(); CommonTreeNodeStream treeNodeStream = new CommonTreeNodeStream(tree); treeNodeStream.setTokenStream(lexerStream); ES3Walker walker = new ES3Walker(treeNodeStream); Executor executor = new Executor(); executor.setBlockManager(context.getBlockManager()); walker.setExecutor(executor); FunctionDescriptor descriptor = walker.functionDescriptor(); return descriptor; }
public static ExecutionContext createGlobalExecutionContext(DynJS runtime) { // 10.4.1.1 LexicalEnvironment env = LexicalEnvironment.newGlobalEnvironment(runtime); ExecutionContext context = new ExecutionContext(null, env, env, env.getGlobalObject(), false); context.clock = runtime.getConfig().getClock(); context.timeZone = runtime.getConfig().getTimeZone(); context.locale = runtime.getConfig().getLocale(); return context; }
@Override public Object call(ExecutionContext context, Object self, Object... args) { // 15.3.2.1 int numArgs = args.length; String body = ""; if (numArgs > 0) { body = Types.toString(context, args[numArgs - 1]); } StringBuffer formalParams = new StringBuffer(); boolean first = true; Set<String> seenParams = new HashSet<>(); boolean duplicateFormalParams = false; for (int i = 0; i < numArgs - 1; ++i) { if (!first) { formalParams.append(","); } String param = Types.toString(context, args[i]); if (seenParams.contains(param)) { duplicateFormalParams = true; } seenParams.add(param); formalParams.append(param); first = false; } StringBuffer code = new StringBuffer(); code.append("function(" + formalParams.toString() + "){\n"); code.append(body); code.append("}"); try { FunctionDescriptor descriptor = parseFunction(context, code.toString()); JSCompiler compiler = context.getGlobalObject().getCompiler(); JSFunction function = compiler.compileFunction( context, descriptor.getFormalParameters(), descriptor.getBlock(), false); if (function.isStrict() && duplicateFormalParams) { throw new ThrowException( context, context.createSyntaxError("duplicate formal parameters in function definition")); } function.setPrototype(getPrototype()); return function; } catch (RecognitionException e) { throw new ThrowException(context, context.createSyntaxError(e.getMessage())); } }
@Override public Object call(ExecutionContext context, Object self, Object... args) { if (!(args[0] instanceof String)) { throw new ThrowException(context, context.createTypeError("Argument must be a string.")); } String string = Types.toString(context, args[0]); return string.getBytes().length; }
@Override public Object call(ExecutionContext context, Object self, Object... args) { JSObject o = Types.toObject(context, self); Object tv = Types.toPrimitive(context, o, "Number"); if (tv instanceof Number) { if (Double.isInfinite(((Number) tv).doubleValue())) { return Types.NULL; } } Object toISO = o.get(context, "toISOString"); if (!(toISO instanceof JSFunction)) { throw new ThrowException(context, context.createTypeError("toISOString must be a function")); } return context.call((JSFunction) toISO, o); }
public void putValue(ExecutionContext context, Object value) { // 8.7.2 if (isUnresolvableReference()) { if (isStrictReference()) { throw new ThrowException( context, context.createReferenceError(referencedName + " is not defined")); } else { context.getGlobalContext().getObject().put(context, this.referencedName, value, false); } } else if (isPropertyReference()) { if (!hasPrimitiveBase()) { ((JSObject) this.base).put(context, this.referencedName, value, this.strict); } else { // TODO: handle primitives } } else { ((EnvironmentRecord) this.base) .setMutableBinding(context, this.referencedName, value, this.strict); } }
@Override public EvaluateResponse handle(EvaluateRequest request) { ExecutionContext context = null; if (request.isGlobal()) { context = this.debugger.getGlobalContext(); } else { context = this.debugger.getContext(request.getFrame()); } Runner runner = context.getRuntime().newRunner(); try { Object result = runner.withContext(context).withSource(request.getExpression()).directEval().evaluate(); return new EvaluateResponse(request, result, true, this.debugger.isRunning()); } catch (ThrowException e) { return new EvaluateResponse(request, Types.UNDEFINED, true, this.debugger.isRunning()); } }
@Override public Object call(ExecutionContext context, Object self, Object... args) { Arguments argsObj = (Arguments) context.resolve("arguments").getValue(context); int numArgs = (int) argsObj.get(context, "length"); if (self != Types.UNDEFINED && self != Types.NULL) { // Constructor String value = ""; if (numArgs != 0) { value = Types.toString(context, args[0]); } PrimitiveDynObject primSelf = (PrimitiveDynObject) self; primSelf.setPrimitiveValue(value); return primSelf; } else { // As function if (numArgs == 0) { return ""; } return Types.toString(context, args[0]); } }
public Object getValue(ExecutionContext context) { // 8.7.1 Object value = null; if (isUnresolvableReference()) { throw new ThrowException( context, context.createReferenceError(referencedName + " is not defined")); } if (isPropertyReference()) { if (!hasPrimitiveBase()) { value = ((JSObject) this.base).get(context, this.referencedName); } else { value = primitiveGet(context, Types.toObject(context, this.base), this.referencedName); } } else { value = ((EnvironmentRecord) this.base) .getBindingValue(context, this.referencedName, this.strict); } return value; }
@Override public Object call(ExecutionContext context, Object self, Object... args) { Number number = 0L; Arguments argsObj = (Arguments) context.resolve("arguments").getValue(context); int numArgs = (int) argsObj.get(context, "length"); if (numArgs != 0) { number = Types.toNumber(context, args[0]); } if (self == Types.UNDEFINED || self == Types.NULL) { // called as a function return number; } else { // called as a ctor PrimitiveDynObject numberObject = (PrimitiveDynObject) self; if (numArgs == 0) { number = 0L; } numberObject.setPrimitiveValue(number); return numberObject; } }
@Override public Object call(ExecutionContext context, Object self, Object... args) { if (!(self instanceof DynDate)) { throw new ThrowException( context, context.createTypeError("setMonth(...) may only be used with Dates")); } DynDate dateObj = (DynDate) self; long t = dateObj.getTimeValue(); Number y = Types.toNumber(context, args[0]); Number m = null; if (args[1] != Types.UNDEFINED) { m = Types.toNumber(context, args[1]); } else { m = monthFromTime(t); } Number dt = null; if (args[1] != Types.UNDEFINED) { dt = Types.toNumber(context, args[2]); } else { dt = dateFromTime(t); } Number newDate = makeDate(context, makeDay(context, yearFromTime(t), m, dt), timeWithinDay(t)); Number u = timeClip(context, newDate); dateObj.setTimeValue(u); return u; }
protected Object primitiveGet(ExecutionContext context, JSObject o, String name) { // 8.7.1 primitive [[Get]] Object d = o.getProperty(context, name, false); if (d == Types.UNDEFINED) { return Types.UNDEFINED; } PropertyDescriptor desc = (PropertyDescriptor) d; if (desc.isDataDescriptor()) { Object value = desc.getValue(); if (value == null) { value = Types.UNDEFINED; } return value; } Object getter = desc.getGetter(); if (getter == Types.UNDEFINED) { return Types.UNDEFINED; } return context.call((JSFunction) getter, o); }
@Override public JSObject createNewObject(ExecutionContext context) { return new DynString(context.getGlobalObject()); }
@Override public JSObject createNewObject(ExecutionContext context) { // 15.7.2.1 return new DynNumber(context.getGlobalObject()); }
public static ExecutionContext createGlobalExecutionContext( DynJS runtime, InitializationListener listener) { ExecutionContext context = ExecutionContext.createEvalExecutionContext(runtime); listener.initialize(context); return context; }
public void collectStackElements(List<StackElement> elements) { elements.add(new StackElement(this.fileName, this.lineNumber, this.debugContext)); if (parent != null) { parent.collectStackElements(elements); } }
@Override public <T> void subscribe( final Observable<T> observable, final Object onNext, final Object onError, final Object onCompleted) { // final ExecutionContext context = ExecutionContext // .createGlobalExecutionContext(((DynJSRuntime)NodynRunner.getRuntime()).runtime); // .createFunctionExecutionContext(((JavascriptFunction)onNext)., // onNext, null, "test"); /* try { context.setFunctionParameters(new Object[] { context.createPropertyReference("testval", "someName") //new DynObject() // GlobalObject.newGlobalObject(NodynRunner.getRuntime()) }); ((JavascriptFunction) onNext).call(context); } catch (final Exception e1) { e1.printStackTrace(); } */ if (observable == null) throw new NullPointerException("Can't subscribe to null-observer"); for (Object callback : Arrays.asList(onNext, onError, onCompleted)) if (callback != null && callback instanceof JavascriptFunction == false) throw new IllegalStateException( "UNEXPECTED callback type: " + callback.getClass().getName()); try { final ExecutionContext context = ExecutionContext.createGlobalExecutionContext( (DynJS) DynJSRuntime.class.getField("runtime").get(NodynRunner.getRuntime())); observable.subscribe( new Observer<T>() { @Override public void onCompleted() { if (onCompleted != null) ((JavascriptFunction) onCompleted).call(context); } @Override public void onError(final Throwable e) { if (onError == null) return; context.setFunctionParameters(new Object[] {e}); ((JavascriptFunction) onError).call(context); } @Override public void onNext(final T t) { if (onNext == null) return; context.setFunctionParameters(new Object[] {t}); ((JavascriptFunction) onNext).call(context); } }); } catch (final Exception e) { LOG.error("failed", e); } }
@Override public Object call(ExecutionContext context, Object self, Object... args) { // 15.3.2.1 int numArgs = args.length; String body = ""; StringBuilder formalParams = new StringBuilder(); boolean first = true; Set<String> seenParams = new HashSet<>(); boolean duplicateFormalParams = false; for (int i = 0; i < numArgs - 1; ++i) { String paramStr = Types.toString(context, args[i]); StringTokenizer paramTokens = new StringTokenizer(paramStr, ","); while (paramTokens.hasMoreTokens()) { if (!first) { formalParams.append(","); } String param = paramTokens.nextToken().trim(); if (seenParams.contains(param)) { duplicateFormalParams = true; } seenParams.add(param); formalParams.append(param); first = false; } } if (numArgs > 0) { body = Types.toString(context, args[numArgs - 1]); } StringBuilder code = new StringBuilder(); code.append("function(" + formalParams.toString() + "){\n"); code.append(body); code.append("}"); try { FunctionDescriptor descriptor = parseFunction(context, code.toString()); JSCompiler compiler = context.getGlobalObject().getCompiler(); JSFunction function = compiler.compileFunction( context, descriptor.getIdentifier(), descriptor.getFormalParameterNames(), descriptor.getBlock(), descriptor.isStrict()); if (function.isStrict()) { if (duplicateFormalParams) { throw new ThrowException( context, context.createSyntaxError("duplicate formal parameters in function definition")); } if (seenParams.contains("eval")) { throw new ThrowException( context, context.createSyntaxError( "formal parameter 'eval' not allowed in function definition in strict-mode")); } } function.setPrototype(getPrototype()); return function; } catch (ParserException e) { throw new ThrowException(context, context.createSyntaxError(e.getMessage())); } catch (IOException e) { throw new ThrowException(context, context.createSyntaxError(e.getMessage())); } }