Добавлена фукнция std::sync

This commit is contained in:
Victor 2016-05-19 18:38:38 +03:00
parent b0396b6105
commit 5e1f52024a
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package com.annimon.ownlang.lib.modules.functions;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public final class std_sync implements Function {
@Override
public Value execute(Value... args) {
Arguments.check(1, args.length);
if (args[0].type() != Types.FUNCTION) {
throw new TypeException(args[0].toString() + " is not a function");
}
final BlockingQueue<Value> queue = new LinkedBlockingQueue<>(2);
final Function synchronizer = (sArgs) -> {
try {
queue.put(sArgs[0]);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
return NumberValue.ZERO;
};
final Function callback = ((FunctionValue) args[0]).getValue();
callback.execute(new FunctionValue(synchronizer));
try {
return queue.take();
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}

View File

@ -18,6 +18,7 @@ public final class std implements Module {
Functions.set("time", new std_time());
Functions.set("sleep", new std_sleep());
Functions.set("thread", new std_thread());
Functions.set("sync", new std_sync());
// String
Functions.set("sprintf", new std_sprintf());