String script = "function abc(x,y) {return x+y;}"; Context context = Context.enter(); try { ScriptableObject scope = context.initStandardObjects(); Scriptable that = context.newObject(scope); Function fct = context.compileFunction(scope, script, "script", 1, null); Object result = fct.call( context, scope, that, new Object[] {2, 3}); System.out.println(Context.jsToJava(result, int.class)); } finally { Context.exit(); }
@Test public void jsToDate_NativeDate() throws Exception { Date date = new Date( 1 ); Scriptable value = ctx.newObject( scope, "Date", new Object[] { date.getTime() } ); assertEquals( date, JavaScriptUtils.jsToDate( value, "org.mozilla.javascript.NativeDate" ) ); }
private Scriptable convertObject(final JSONObject obj) throws JSONException { if (obj == JSONObject.NULL) { return null; } final Scriptable result = context.newObject(scope); final Iterator<?> it = obj.keys(); while (it.hasNext()) { final String key = (String) it.next(); final Object value = obj.get(key); ScriptableObject.putProperty(result, key, convert(value)); } return result; }
/** * Create a new JavaScript object. * * Equivalent to evaluating "new Object()". * @param scope the scope to search for the constructor and to evaluate * against * @return the new object */ public final Scriptable newObject(Scriptable scope) { return newObject(scope, "Object", ScriptRuntime.emptyArgs); }
private NativeArrayBuffer makeArrayBuffer(Context cx, Scriptable scope, int length) { return (NativeArrayBuffer)cx.newObject(scope, NativeArrayBuffer.CLASS_NAME, new Object[] { length }); }
/** * Create a new JavaScript object. * * Equivalent to evaluating "new Object()". * @param scope the scope to search for the constructor and to evaluate * against * @return the new object */ public final Scriptable newObject(Scriptable scope) { return newObject(scope, "Object", ScriptRuntime.emptyArgs); }
public Object run(Context cx) { applyDefaultOptions(); Scriptable opts = cx.newObject(lintFunc); for (Entry<Option, Object> entry : options.entrySet()) { String key = entry.getKey().getLowerName(); // Use our "custom" version in order to get native arrays. Object value = Util.javaToJS(entry.getValue(), opts); opts.put(key, opts, value); } return opts; } });
public static Scriptable jsStaticFunction_readXls(final Context ctx, final Scriptable object, final Object[] args, final Function func) throws IOException { final String file = Context.toString(args[0]); final DataFrame<Object> df = DataFrame.readXls(file); return new DataFrameAdapter(ctx.newObject(object, df.getClass().getSimpleName()), df); }
@Override public Scriptable registerExports(Context cx, Scriptable global, NodeRuntime runtime) throws InvocationTargetException, IllegalAccessException, InstantiationException { ScriptableObject.defineClass(global, XmlWrapImpl.class); XmlWrapImpl wrap = (XmlWrapImpl)cx.newObject(global, XmlWrapImpl.CLASS_NAME); wrap.initTransformer(cx, runtime); return wrap; }
public static Scriptable jsStaticFunction_readCsv(final Context ctx, final Scriptable object, final Object[] args, final Function func) throws IOException { final String file = Context.toString(args[0]); final DataFrame<Object> df = DataFrame.readCsv(file); return new DataFrameAdapter(ctx.newObject(object, df.getClass().getSimpleName()), df); }
@Override public Scriptable registerExports(Context cx, Scriptable global, NodeRuntime runtime) throws InvocationTargetException, IllegalAccessException, InstantiationException { ScriptableObject.defineClass(global, IconvModuleImpl.class); Scriptable exports = cx.newObject(global, IconvModuleImpl.CLASS_NAME); ScriptableObject.defineClass(exports, IconvImpl.class); return exports; }
static ElementArray newElementArray( Scriptable parent ) { try { return (ElementArray) Context.getCurrentContext().newObject( parent, "ElementArray" ); } catch (PropertyException e) { throw new RhinoException( e ); } catch (NotAFunctionException e) { throw new RhinoException( e ); } catch (JavaScriptException e) { throw new RhinoException( e ); } }
@JSFunction @SuppressWarnings("unused") public static Object createDocument(Context cx, Scriptable thisObj, Object[] args, Function funObj) { ensureArg(args, 0); XmlWrapImpl self = (XmlWrapImpl)thisObj; Source src = self.getSource(cx, args[0]); ScriptableObject ret = (ScriptableObject)cx.newObject(thisObj); ret.associateValue("document", src); return ret; }
public Scriptable jsGet_frames() throws SAXException, PropertyException, JavaScriptException, NotAFunctionException { if (_frames == null) { WebResponse.Scriptable scriptables[] = getDelegate().getFrames(); Window[] frames = new Window[ scriptables.length ]; for (int i = 0; i < frames.length; i++) { frames[ i ] = (Window) toScriptable( scriptables[ i ] ); } _frames = (ElementArray) Context.getCurrentContext().newObject( this, "ElementArray" ); _frames.initialize( frames ); } return _frames; }
private void initializeControls() throws PropertyException, NotAFunctionException, JavaScriptException { ScriptableDelegate scriptables[] = getDelegate().getElementDelegates(); Control[] controls = new Control[ scriptables.length ]; for (int i = 0; i < controls.length; i++) { controls[ i ] = (Control) toScriptable( scriptables[ i ] ); } _controls = (ElementArray) Context.getCurrentContext().newObject( this, "ElementArray" ); _controls.initialize( controls ); }
public static void init(Context cx, Scriptable scope, boolean sealed) throws RhinoException { JSAdapter obj = new JSAdapter(cx.newObject(scope)); obj.setParentScope(scope); obj.setPrototype(getFunctionPrototype(scope)); obj.isPrototype = true; ScriptableObject.defineProperty(scope, "JSAdapter", obj, ScriptableObject.DONTENUM); }
public Scriptable jsGet_frames() throws SAXException, PropertyException, JavaScriptException, NotAFunctionException { if (_frames == null) { WebResponse.Scriptable scriptables[] = getDelegate().getFrames(); Window[] frames = new Window[ scriptables.length ]; for (int i = 0; i < frames.length; i++) { frames[ i ] = (Window) toScriptable( scriptables[ i ] ); } _frames = (ElementArray) Context.getCurrentContext().newObject( this, "ElementArray" ); _frames.initialize( frames ); } return _frames; }
@Override public Scriptable registerExports(Context cx, Scriptable global, NodeRuntime runtime) throws InvocationTargetException, IllegalAccessException, InstantiationException { ScriptableObject.defineClass(global, Referenceable.class); ScriptableObject.defineClass(global, JdbcImpl.class); ScriptableObject.defineClass(global, JdbcConnection.class, false, true); ScriptableObject.defineClass(global, JdbcResultHandle.class); JdbcImpl impl = (JdbcImpl)cx.newObject(global, JdbcImpl.CLASS_NAME); impl.init(runtime); return impl; }
public static void init(Context cx, Scriptable scope, boolean sealed) throws RhinoException { JSAdapter obj = new JSAdapter(cx.newObject(scope)); obj.setParentScope(scope); obj.setPrototype(getFunctionPrototype(scope)); obj.isPrototype = true; ScriptableObject.defineProperty(scope, "JSAdapter", obj, ScriptableObject.DONTENUM); }
public Scriptable jsGet_frames() throws SAXException, PropertyException, JavaScriptException, NotAFunctionException { if (_frames == null) { WebResponse.Scriptable scriptables[] = getDelegate().getFrames(); Window[] frames = new Window[ scriptables.length ]; for (int i = 0; i < frames.length; i++) { frames[ i ] = (Window) toScriptable( scriptables[ i ] ); } _frames = (ElementArray) Context.getCurrentContext().newObject( this, "ElementArray" ); _frames.initialize( frames ); } return _frames; }