@Override public void invoke(ExecutionContext context) throws ResolvedControlThrowable { System.out.println("invoke"); try { context.resumeAfter(new AsyncTask() { @Override public void execute(ContinueCallback callback) { System.out.println("in task"); callback.finished(); } }); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, null); } // control never reaches this point System.out.println("after async -- not!"); }
@Override public void pause() throws UnresolvedControlThrowable { throw new UnresolvedControlThrowable(PAUSED_PAYLOAD); }
@Override public void invoke(ExecutionContext context, Object id, Object arg2) throws ResolvedControlThrowable { @SuppressWarnings("unchecked") final BlockingQueue<Continuation> workQueue = (BlockingQueue<Continuation>) arg2; System.out.println("[" + Thread.currentThread() + "]: Invoke " + id); AtomicInteger var = new AtomicInteger(0); try { context.resumeAfter(new SleepAndSpawn(id, var, context, workQueue)); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, new Object[] {id, var}); } throw new AssertionError(); // control never reaches this point }
UnresolvedControlThrowable unresolve() { return new UnresolvedControlThrowable(payload, resumeStack); }
@Override public void invoke(ExecutionContext context, Object[] args) throws ResolvedControlThrowable { try { Dispatch.call(context, x); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, Arrays.copyOf(args, args.length)); } resume(context, args); }
@Override public void resume(Coroutine coroutine, Object[] args) throws UnresolvedControlThrowable { Objects.requireNonNull(coroutine); throw new UnresolvedControlThrowable(new ControlPayload( false, Objects.requireNonNull(coroutine), Objects.requireNonNull(args), null)); }
private void load(ExecutionContext context, ByteString modName, LuaFunction loader, Object origin) throws ResolvedControlThrowable { try { Dispatch.call(context, loader, modName, origin); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, modName); } resumeLoad(context, modName); }
@Override public void yield(Object[] args) throws UnresolvedControlThrowable { throw new UnresolvedControlThrowable(new ControlPayload( false, null, Objects.requireNonNull(args), null)); }
@Override public void invoke(ExecutionContext context, Object arg1, Object arg2, Object arg3) throws ResolvedControlThrowable { try { // b + c Dispatch.add(context, arg2, arg3); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, arg1); } resume(context, arg1); }
@Override public void resumeAfter(AsyncTask task) throws UnresolvedControlThrowable { throw new UnresolvedControlThrowable(new ControlPayload( false, null, null, Objects.requireNonNull(task))); }
@Override public void invoke(ExecutionContext context, Object t, Object k) throws ResolvedControlThrowable { try { Dispatch.index(context, t, k); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, null); } resume(context, null); }
@Override protected void invoke(ExecutionContext context, ArgumentIterator args) throws ResolvedControlThrowable { IoFile file = lib.getDefaultInputFile(); ArrayList<Object> callArgs = new ArrayList<>(); callArgs.add(file); callArgs.addAll(Arrays.asList(args.copyAll())); try { Dispatch.call(context, file_read(), callArgs.toArray()); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, null); } resume(context, file); }
@Override protected void invoke(ExecutionContext context, ArgumentIterator args) throws ResolvedControlThrowable { try { context.yield(args.copyAll()); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, null); } }
@Override public void invoke(ExecutionContext context, Object t, Object k, Object v) throws ResolvedControlThrowable { try { Dispatch.setindex(context, t, k, v); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, null); } resume(context, null); }
@Override protected void invoke(ExecutionContext context, ArgumentIterator args) throws ResolvedControlThrowable { IoFile file = lib.getDefaultOutputFile(); ArrayList<Object> callArgs = new ArrayList<>(); callArgs.add(file); callArgs.addAll(Arrays.asList(args.copyAll())); try { Dispatch.call(context, file_write(), callArgs.toArray()); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, null); } resume(context, file); }
@Override public void resume(ExecutionContext context, Object target) throws ResolvedControlThrowable { try { Dispatch.call(context, target, context.getReturnBuffer().getAsArray()); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(); } }
@Override public void invoke(ExecutionContext context, Object[] args) throws ResolvedControlThrowable { context.getReturnBuffer().setTo(); try { context.resume(coroutine, args); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, null); } }
@Override protected void invoke(ExecutionContext context, ArgumentIterator args) throws ResolvedControlThrowable { IoFile outFile = lib.getDefaultOutputFile(); try { Dispatch.call(context, file_flush()); } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, outFile); } resume(context, outFile); }
private void nonStringReplace( ExecutionContext context, String str, StringPattern pat, int idx, int count, int num, StringBuilder bld, Object repl, String fullMatch, List<Object> captures) throws ResolvedControlThrowable { assert (!captures.isEmpty()); Object cap = captures.get(0); try { if (repl instanceof Table) { Dispatch.index(context, (Table) repl, cap); } else if (repl instanceof LuaFunction) { Dispatch.call(context, (LuaFunction) repl, (Object[]) captures.toArray()); } else { throw new IllegalStateException("Illegal replacement: " + repl); } } catch (UnresolvedControlThrowable ct) { throw ct.resolve(this, new State(str, pat, count, num, repl, bld, fullMatch, idx)); } resumeReplace(context, bld, fullMatch); }
private void _erase(ExecutionContext context, int state, Table t, long idx, Object result) throws ResolvedControlThrowable { try { switch (state) { case _ERASE_PREPARE: state = _ERASE_RESUME; Dispatch.setindex(context, t, idx, null); case _ERASE_RESUME: // we're done break; default: throw new IllegalStateException("Illegal state: " + state); } } catch (UnresolvedControlThrowable ct) { // no need to carry any information but the result around throw ct.resolve(this, new SuspendedState(state, null, null, 0, 0, result)); } // finished, set the result context.getReturnBuffer().setTo(result); }