Compare commits
4 Commits
0.9.33-bet
...
0.9.37-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
d6ede0b404
|
|||
|
71b40240c0
|
|||
|
a8775d212f
|
|||
|
71872a8d64
|
@@ -1,5 +1,7 @@
|
|||||||
package me.topchetoeu.jscript.lib;
|
package me.topchetoeu.jscript.lib;
|
||||||
|
|
||||||
|
import java.text.NumberFormat;
|
||||||
|
|
||||||
import me.topchetoeu.jscript.runtime.values.ObjectValue;
|
import me.topchetoeu.jscript.runtime.values.ObjectValue;
|
||||||
import me.topchetoeu.jscript.runtime.values.Values;
|
import me.topchetoeu.jscript.runtime.values.Values;
|
||||||
import me.topchetoeu.jscript.utils.interop.Arguments;
|
import me.topchetoeu.jscript.utils.interop.Arguments;
|
||||||
@@ -85,6 +87,15 @@ public class NumberLib {
|
|||||||
@Expose public static String __toString(Arguments args) {
|
@Expose public static String __toString(Arguments args) {
|
||||||
return Values.toString(args.ctx, args.self);
|
return Values.toString(args.ctx, args.self);
|
||||||
}
|
}
|
||||||
|
@Expose public static String __toFixed(Arguments args) {
|
||||||
|
var digits = args.getInt(0, 0);
|
||||||
|
|
||||||
|
var nf = NumberFormat.getNumberInstance();
|
||||||
|
nf.setMinimumFractionDigits(digits);
|
||||||
|
nf.setMaximumFractionDigits(digits);
|
||||||
|
|
||||||
|
return nf.format(args.getDouble(-1));
|
||||||
|
}
|
||||||
@Expose public static double __valueOf(Arguments args) {
|
@Expose public static double __valueOf(Arguments args) {
|
||||||
if (Values.isWrapper(args.self, NumberLib.class)) return Values.wrapper(args.self, NumberLib.class).value;
|
if (Values.isWrapper(args.self, NumberLib.class)) return Values.wrapper(args.self, NumberLib.class).value;
|
||||||
else return Values.toNumber(args.ctx, args.self);
|
else return Values.toNumber(args.ctx, args.self);
|
||||||
|
|||||||
@@ -14,43 +14,26 @@ public class Context implements Extensions {
|
|||||||
public final Context parent;
|
public final Context parent;
|
||||||
public final Extensions extensions;
|
public final Extensions extensions;
|
||||||
public final Frame frame;
|
public final Frame frame;
|
||||||
// public final Engine engine;
|
|
||||||
public final int stackSize;
|
public final int stackSize;
|
||||||
|
|
||||||
@Override public <T> void add(Key<T> key, T obj) {
|
@Override public <T> void add(Key<T> key, T obj) {
|
||||||
if (extensions != null) extensions.add(key, obj);
|
if (extensions != null) extensions.add(key, obj);
|
||||||
// else if (engine != null) engine.add(key, obj);
|
|
||||||
}
|
}
|
||||||
@Override public <T> T get(Key<T> key) {
|
@Override public <T> T get(Key<T> key) {
|
||||||
if (extensions != null && extensions.has(key)) return extensions.get(key);
|
if (extensions != null && extensions.has(key)) return extensions.get(key);
|
||||||
// else if (engine != null && engine.has(key)) return engine.get(key);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@Override public boolean has(Key<?> key) {
|
@Override public boolean has(Key<?> key) {
|
||||||
return
|
return extensions != null && extensions.has(key);
|
||||||
extensions != null && extensions.has(key);
|
|
||||||
// engine != null && engine.has(key);
|
|
||||||
}
|
}
|
||||||
@Override public boolean remove(Key<?> key) {
|
@Override public boolean remove(Key<?> key) {
|
||||||
var res = false;
|
var res = false;
|
||||||
|
|
||||||
if (extensions != null) res |= extensions.remove(key);
|
if (extensions != null) res |= extensions.remove(key);
|
||||||
// else if (engine != null) res |= engine.remove(key);
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@Override public Iterable<Key<?>> keys() {
|
@Override public Iterable<Key<?>> keys() {
|
||||||
if (extensions == null) return List.of();
|
if (extensions == null) return List.of();
|
||||||
else return extensions.keys();
|
else return extensions.keys();
|
||||||
|
|
||||||
// if (engine == null && environment == null) return List.of();
|
|
||||||
// if (engine == null) return environment.keys();
|
|
||||||
// if (environment == null) return engine.keys();
|
|
||||||
|
|
||||||
// return () -> Stream.concat(
|
|
||||||
// StreamSupport.stream(engine.keys().spliterator(), false),
|
|
||||||
// StreamSupport.stream(environment.keys().spliterator(), false)
|
|
||||||
// ).distinct().iterator();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public FunctionValue compile(Filename filename, String raw) {
|
public FunctionValue compile(Filename filename, String raw) {
|
||||||
@@ -101,7 +84,7 @@ public class Context implements Extensions {
|
|||||||
this(null, null, null, 0);
|
this(null, null, null, 0);
|
||||||
}
|
}
|
||||||
public Context(Extensions ext) {
|
public Context(Extensions ext) {
|
||||||
this(null, ext, null, 0);
|
this(null, clean(ext), null, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Context of(Extensions ext) {
|
public static Context of(Extensions ext) {
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ public class CodeFunction extends FunctionValue {
|
|||||||
public CodeFunction(Extensions extensions, String name, FunctionBody body, ValueVariable[] captures) {
|
public CodeFunction(Extensions extensions, String name, FunctionBody body, ValueVariable[] captures) {
|
||||||
super(name, body.argsN);
|
super(name, body.argsN);
|
||||||
this.captures = captures;
|
this.captures = captures;
|
||||||
this.extensions = extensions;
|
this.extensions = Context.clean(extensions);
|
||||||
this.body = body;
|
this.body = body;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import me.topchetoeu.jscript.runtime.debug.DebugContext;
|
|||||||
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
|
import me.topchetoeu.jscript.runtime.exceptions.EngineException;
|
||||||
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
|
import me.topchetoeu.jscript.runtime.exceptions.InterruptException;
|
||||||
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
|
import me.topchetoeu.jscript.runtime.exceptions.SyntaxException;
|
||||||
|
import me.topchetoeu.jscript.runtime.scope.GlobalScope;
|
||||||
import me.topchetoeu.jscript.runtime.values.NativeFunction;
|
import me.topchetoeu.jscript.runtime.values.NativeFunction;
|
||||||
import me.topchetoeu.jscript.runtime.values.Values;
|
import me.topchetoeu.jscript.runtime.values.Values;
|
||||||
import me.topchetoeu.jscript.utils.debug.DebugServer;
|
import me.topchetoeu.jscript.utils.debug.DebugServer;
|
||||||
@@ -28,6 +29,7 @@ import me.topchetoeu.jscript.utils.filesystem.Mode;
|
|||||||
import me.topchetoeu.jscript.utils.filesystem.PhysicalFilesystem;
|
import me.topchetoeu.jscript.utils.filesystem.PhysicalFilesystem;
|
||||||
import me.topchetoeu.jscript.utils.filesystem.RootFilesystem;
|
import me.topchetoeu.jscript.utils.filesystem.RootFilesystem;
|
||||||
import me.topchetoeu.jscript.utils.filesystem.STDFilesystem;
|
import me.topchetoeu.jscript.utils.filesystem.STDFilesystem;
|
||||||
|
import me.topchetoeu.jscript.utils.interop.NativeWrapperProvider;
|
||||||
import me.topchetoeu.jscript.utils.modules.ModuleRepo;
|
import me.topchetoeu.jscript.utils.modules.ModuleRepo;
|
||||||
import me.topchetoeu.jscript.utils.permissions.PermissionsManager;
|
import me.topchetoeu.jscript.utils.permissions.PermissionsManager;
|
||||||
import me.topchetoeu.jscript.utils.permissions.PermissionsProvider;
|
import me.topchetoeu.jscript.utils.permissions.PermissionsProvider;
|
||||||
@@ -87,10 +89,13 @@ public class JScriptRepl {
|
|||||||
private static void initEnv() {
|
private static void initEnv() {
|
||||||
environment = Internals.apply(environment);
|
environment = Internals.apply(environment);
|
||||||
|
|
||||||
environment.global.define(null, false, new NativeFunction("exit", args -> {
|
var wp = NativeWrapperProvider.get(environment);
|
||||||
|
var glob = GlobalScope.get(environment);
|
||||||
|
|
||||||
|
glob.define(null, false, new NativeFunction("exit", args -> {
|
||||||
throw new InterruptException();
|
throw new InterruptException();
|
||||||
}));
|
}));
|
||||||
environment.global.define(null, false, new NativeFunction("go", args -> {
|
glob.define(null, false, new NativeFunction("go", args -> {
|
||||||
try {
|
try {
|
||||||
var f = Path.of("do.js");
|
var f = Path.of("do.js");
|
||||||
var func = args.ctx.compile(new Filename("do", "do/" + j++ + ".js"), new String(Files.readAllBytes(f)));
|
var func = args.ctx.compile(new Filename("do", "do/" + j++ + ".js"), new String(Files.readAllBytes(f)));
|
||||||
@@ -100,7 +105,7 @@ public class JScriptRepl {
|
|||||||
throw new EngineException("Couldn't open do.js");
|
throw new EngineException("Couldn't open do.js");
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
environment.global.define(null, false, new NativeFunction("log", args -> {
|
glob.define(null, false, new NativeFunction("log", args -> {
|
||||||
for (var el : args.args) {
|
for (var el : args.args) {
|
||||||
Values.printValue(args.ctx, el);
|
Values.printValue(args.ctx, el);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user