mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Добавлена фукнция std::sync
This commit is contained in:
parent
b0396b6105
commit
5e1f52024a
36
src/com/annimon/ownlang/lib/modules/functions/std_sync.java
Normal file
36
src/com/annimon/ownlang/lib/modules/functions/std_sync.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -18,6 +18,7 @@ public final class std implements Module {
|
|||||||
Functions.set("time", new std_time());
|
Functions.set("time", new std_time());
|
||||||
Functions.set("sleep", new std_sleep());
|
Functions.set("sleep", new std_sleep());
|
||||||
Functions.set("thread", new std_thread());
|
Functions.set("thread", new std_thread());
|
||||||
|
Functions.set("sync", new std_sync());
|
||||||
|
|
||||||
// String
|
// String
|
||||||
Functions.set("sprintf", new std_sprintf());
|
Functions.set("sprintf", new std_sprintf());
|
||||||
|
Loading…
Reference in New Issue
Block a user