ES6 Object and assignment destructors + object stuff #28

Merged
TopchetoEU merged 15 commits from TopchetoEU/destructing into master 2024-09-14 12:38:02 +00:00
4 changed files with 23 additions and 16 deletions
Showing only changes of commit 5f88061ee7 - Show all commits

View File

@ -28,9 +28,9 @@ public interface EventLoop {
} }
public default Future<Value> pushMsg(boolean micro, Environment env, FunctionValue func, Value thisArg, Value ...args) { public default Future<Value> pushMsg(boolean micro, Environment env, FunctionValue func, Value thisArg, Value ...args) {
return pushMsg(() -> func.call(env, thisArg, args), micro); return pushMsg(() -> func.invoke(env, thisArg, args), micro);
} }
public default Future<Value> pushMsg(boolean micro, Environment env, Filename filename, String raw, Value thisArg, Value ...args) { public default Future<Value> pushMsg(boolean micro, Environment env, Filename filename, String raw, Value thisArg, Value ...args) {
return pushMsg(() -> Compiler.compileFunc(env, filename, raw).call(env, thisArg, args), micro); return pushMsg(() -> Compiler.compileFunc(env, filename, raw).invoke(env, thisArg, args), micro);
} }
} }

View File

@ -51,7 +51,7 @@ public class InstructionRunner {
var callArgs = frame.take(instr.get(0)); var callArgs = frame.take(instr.get(0));
var funcObj = frame.pop(); var funcObj = frame.pop();
frame.push(funcObj.callNew(env, instr.get(1), callArgs)); frame.push(funcObj.construct(env, instr.get(1), callArgs));
frame.codePtr++; frame.codePtr++;
return null; return null;
@ -82,7 +82,7 @@ public class InstructionRunner {
private static Value execKeys(Environment env, Instruction instr, Frame frame) { private static Value execKeys(Environment env, Instruction instr, Frame frame) {
var val = frame.pop(); var val = frame.pop();
var members = new ArrayList<>(val.getMembers(env, false, true)); var members = new ArrayList<>(val.getMembers(env, instr.get(0), instr.get(1)));
Collections.reverse(members); Collections.reverse(members);
frame.push(null); frame.push(null);
@ -115,9 +115,12 @@ public class InstructionRunner {
private static Value execDup(Environment env, Instruction instr, Frame frame) { private static Value execDup(Environment env, Instruction instr, Frame frame) {
int count = instr.get(0); int count = instr.get(0);
int offset = instr.get(1);
var el = frame.stack[frame.stackPtr - offset - 1];
for (var i = 0; i < count; i++) { for (var i = 0; i < count; i++) {
frame.push(frame.peek()); frame.push(el);
} }
frame.codePtr++; frame.codePtr++;
@ -230,7 +233,7 @@ public class InstructionRunner {
} }
private static Value execLoadRegEx(Environment env, Instruction instr, Frame frame) { private static Value execLoadRegEx(Environment env, Instruction instr, Frame frame) {
if (env.hasNotNull(Value.REGEX_CONSTR)) { if (env.hasNotNull(Value.REGEX_CONSTR)) {
frame.push(env.get(Value.REGEX_CONSTR).callNew(env, instr.get(0), instr.get(1))); frame.push(env.get(Value.REGEX_CONSTR).construct(env, instr.get(0), instr.get(1)));
} }
else { else {
throw EngineException.ofSyntax("Regex is not supported."); throw EngineException.ofSyntax("Regex is not supported.");

View File

@ -83,7 +83,11 @@ public abstract class Value {
if (isNew) throw EngineException.ofType(name + " is not a constructor"); if (isNew) throw EngineException.ofType(name + " is not a constructor");
else throw EngineException.ofType(name + " is not a function"); else throw EngineException.ofType(name + " is not a function");
} }
public final Value callNew(Environment env, String name, Value ...args) {
public final Value invoke(Environment env, String name, Value self, Value ...args) {
return call(env, false, name, self, args);
}
public final Value construct(Environment env, String name, Value ...args) {
var res = new ObjectValue(); var res = new ObjectValue();
var proto = getMember(env, new StringValue("prototype")); var proto = getMember(env, new StringValue("prototype"));
@ -96,11 +100,11 @@ public abstract class Value {
return res; return res;
} }
public final Value call(Environment env, Value self, Value ...args) { public final Value invoke(Environment env, Value self, Value ...args) {
return call(env, false, "", self, args); return invoke(env, "", self, args);
} }
public final Value callNew(Environment env, Value ...args) { public final Value construct(Environment env, Value ...args) {
return callNew(env, "", args); return construct(env, "", args);
} }
public abstract Value toPrimitive(Environment env); public abstract Value toPrimitive(Environment env);
@ -378,7 +382,7 @@ public abstract class Value {
private void loadNext() { private void loadNext() {
if (supplier == null) value = null; if (supplier == null) value = null;
else if (consumed) { else if (consumed) {
var curr = supplier.call(env, Value.UNDEFINED); var curr = supplier.invoke(env, Value.UNDEFINED);
if (curr == null) { supplier = null; value = null; } if (curr == null) { supplier = null; value = null; }
if (curr.getMember(env, new StringValue("done")).toBoolean()) { supplier = null; value = null; } if (curr.getMember(env, new StringValue("done")).toBoolean()) { supplier = null; value = null; }
@ -406,12 +410,12 @@ public abstract class Value {
public void callWith(Environment env, Iterable<? extends Value> it) { public void callWith(Environment env, Iterable<? extends Value> it) {
for (var el : it) { for (var el : it) {
this.call(env, Value.UNDEFINED, el); this.invoke(env, Value.UNDEFINED, el);
} }
} }
public void callWithAsync(Environment env, Iterable<? extends Value> it, boolean async) { public void callWithAsync(Environment env, Iterable<? extends Value> it, boolean async) {
for (var el : it) { for (var el : it) {
env.get(EventLoop.KEY).pushMsg(() -> this.call(env, Value.UNDEFINED, el), true); env.get(EventLoop.KEY).pushMsg(() -> this.invoke(env, Value.UNDEFINED, el), true);
} }
} }

View File

@ -52,13 +52,13 @@ public class ObjectValue extends Value {
var valueOf = getMember(env, new StringValue("valueOf")); var valueOf = getMember(env, new StringValue("valueOf"));
if (valueOf instanceof FunctionValue) { if (valueOf instanceof FunctionValue) {
var res = valueOf.call(env, this); var res = valueOf.invoke(env, this);
if (res.isPrimitive()) return res; if (res.isPrimitive()) return res;
} }
var toString = getMember(env, new StringValue("toString")); var toString = getMember(env, new StringValue("toString"));
if (toString instanceof FunctionValue) { if (toString instanceof FunctionValue) {
var res = toString.call(env, this); var res = toString.invoke(env, this);
if (res.isPrimitive()) return res; if (res.isPrimitive()) return res;
} }
} }