@Override public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, Object[] args) { final Function callback = (Function) args[0]; if (callback != null ) { asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { return callback.call(cx, scope, thisObj, new Object[] {}); } }); } return Undefined.instance; } }
private Object mkdir(final Context cx, final Scriptable scope, final Scriptable thisObj, String file, final Function callback) { new File(file).mkdir(); if (callback != null) { asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { return callback.call(cx, scope, thisObj, new Object[] {null}); } }); } return Undefined.instance; } }
static void init(Scriptable scope, boolean sealed) { BaseFunction obj = new BaseFunction(); // Function.prototype attributes: see ECMA 15.3.3.1 obj.prototypePropertyAttributes = DONTENUM | READONLY | PERMANENT; obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); }
static void init(Scriptable scope, boolean sealed) { BaseFunction obj = new BaseFunction(); // Function.prototype attributes: see ECMA 15.3.3.1 obj.prototypePropertyAttributes = DONTENUM | READONLY | PERMANENT; obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); }
static void init(Scriptable scope, boolean sealed) { BaseFunction obj = new BaseFunction(); // Function.prototype attributes: see ECMA 15.3.3.1 obj.prototypePropertyAttributes = DONTENUM | READONLY | PERMANENT; obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); }
static void init(Scriptable scope, boolean sealed) { BaseFunction obj = new BaseFunction(); // Function.prototype attributes: see ECMA 15.3.3.1 obj.prototypePropertyAttributes = DONTENUM | READONLY | PERMANENT; obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); }
static void init(Scriptable scope, boolean sealed) { BaseFunction obj = new BaseFunction(); // Function.prototype attributes: see ECMA 15.3.3.1 obj.prototypePropertyAttributes = DONTENUM | READONLY | PERMANENT; obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); }
static void init(Scriptable scope, boolean sealed) { BaseFunction obj = new BaseFunction(); // Function.prototype attributes: see ECMA 15.3.3.1 obj.prototypePropertyAttributes = DONTENUM | READONLY | PERMANENT; obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); }
private Object writeFile(final Context cx, final Scriptable scope, final Scriptable thisObj, String file, String data, final Function callback) { try { FileUtils.write(new File(file).getAbsoluteFile(), data, Charset.forName("UTF-8")); } catch (IOException e) { throw new RuntimeException(e); } if (callback != null) { asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { return callback.call(cx, scope, thisObj, new Object[] {null}); } }); } return Undefined.instance; } }
private Object readFile(final Context cx, final Scriptable scope, final Scriptable thisObj, String file, String encoding,final Function callback) { final String result; try { result = FileUtils.readFileToString(new File(file).getAbsoluteFile(), encoding); } catch (IOException e) { if (callback != null) { asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { return callback.call(cx, scope, thisObj, new Object[] {true}); } }); } return Undefined.instance; } if (callback != null) { asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { return callback.call(cx, scope, thisObj, new Object[] {null, result}); } }); } return Undefined.instance; }
@Override public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, Object[] args) { if (args.length != 2) { throw new IllegalArgumentException("Only 2 parameters supported"); } final Function callback = (Function) args[1]; if (args[0].equals("exit")) { exitCallbackExecutor.add(new BaseFunction() { @Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { return callback.call(cx, scope, thisObj, new Object[]{0}); } }); } return Undefined.instance; } });
@Override public Object call(final Context cx,final Scriptable scope, final Scriptable thisObj, Object[] args) { if ( args.length != 1 && args.length != 2) { throw new RuntimeException("Only exists with 1 and 2 parameters supported"); } final File file = new File(Context.toString(args[0])).getAbsoluteFile(); final Function callback = (Function) args[1]; if (callback != null) { asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { return callback.call(cx, scope, thisObj, new Object[] {Context.javaToJS(file.exists(), scope)}); } }); } return Undefined.instance; } }
@Override public Object call(final Context cx,final Scriptable scope, final Scriptable thisObj, Object[] args) { if ( args.length != 2) { throw new RuntimeException("Only stat 2 parameters supported"); } final File file = new File(Context.toString(args[0])); final Function callback = (Function) (args[1]); if (callback != null) { asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { return callback.call(cx, scope, thisObj, new Object[] {null, Context.javaToJS(new Stats(file), scope)}); } }); } return Undefined.instance; } }
asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
@Override public Object call(final Context cx,final Scriptable scope, final Scriptable thisObj, Object[] args) { if ( args.length != 2) { throw new RuntimeException("Only readdir 2 parameters supported"); } final File file = new File(Context.toString(args[0])).getAbsoluteFile(); final Function callback = (Function) args[1]; if (callback != null) { asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { String[] list = file.list(); if ( list == null ) { return callback.call(cx, scope, thisObj, new Object[] {null, Undefined.instance}); } Object[] objects = new Object[list.length]; System.arraycopy(list, 0, objects, 0, list.length); return callback.call(cx, scope, thisObj, new Object[] {null, cx.newArray(scope, objects)}); } }); } return Undefined.instance; } }
@Override public Object call(final Context cx,final Scriptable scope, final Scriptable thisObj, Object[] args) { if ( args.length != 2) { throw new RuntimeException("Only realpath 2 parameters supported"); } final File file = new File(Context.toString(args[0])); final Function callback = (Function) (args[1]); if (callback != null) { asyncCallbacksQueue.add(new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { try { return callback.call(cx, scope, thisObj, new Object[] {null, Context.javaToJS(file.getCanonicalPath(), scope)}); } catch (IOException e) { return callback.call(cx, scope, thisObj, new Object[] {true}); } } }); } return Undefined.instance; } }
final Function emit = ScriptableObject.getTypedProperty(originalThisObj, "emit", Function.class); BaseFunction readLineCallback = new BaseFunction() {
@Override public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) { final Function callback = (Function) args[0]; final Long millisToWait = (Long) Context.jsToJava(args[1], Long.class); final long startTime = System.currentTimeMillis(); final Timer timer = new Timer(); if (callback != null) { final BaseFunction timerFunction = new BaseFunction() { @Override public Object call(Context cx2, Scriptable scope2, Scriptable thisObj2, Object[] args2) { if (System.currentTimeMillis() >= startTime + millisToWait) { if ( timer.shouldExecute() ) { return callback.call(cx, scope, thisObj, args); } else { return Undefined.instance; } } asyncCallbacksQueue.add(this); return Undefined.instance; } }; asyncCallbacksQueue.add(timerFunction); } return Context.javaToJS(timer, scope); } }
final BaseFunction timerFunction = new BaseFunction() {
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { String s = Context.toString(args[0]); if (s.equals("buffer")) { Scriptable buffer = cx.newObject(scope); Scriptable slowBuffer = new BaseFunction(); ScriptableObject.putProperty(slowBuffer, "makeFastBuffer", new BaseFunction()); slowBuffer.setPrototype(new BaseFunction()); ScriptableObject.putProperty(slowBuffer, "prototype", slowBuffer.getPrototype()); ScriptableObject.putProperty(buffer, "SlowBuffer", slowBuffer); return buffer; } else if (s.equals("evals")) { Scriptable nodeScript = cx.newObject(scope); ScriptableObject.putProperty(nodeScript, "runInThisContext", new RunInContext(asyncFunctionQueue)); ScriptableObject.putProperty(nodeScript, "runInNewContext", new RunInNewContext(asyncFunctionQueue)); Scriptable evals = cx.newObject(scope); ScriptableObject.putProperty(evals, "NodeScript", nodeScript); return evals; } else { throw new RuntimeException("Not implemented: process.binding for " + s + " does not exist."); } } }