|
1 | 1 | package me.topchetoeu.jscript.lib; |
2 | 2 |
|
| 3 | +import me.topchetoeu.jscript.common.Filename; |
| 4 | +import me.topchetoeu.jscript.runtime.Compiler; |
| 5 | +import me.topchetoeu.jscript.runtime.Context; |
| 6 | +import me.topchetoeu.jscript.runtime.scope.ValueVariable; |
3 | 7 | import me.topchetoeu.jscript.runtime.values.ArrayValue; |
4 | 8 | import me.topchetoeu.jscript.runtime.values.CodeFunction; |
5 | 9 | import me.topchetoeu.jscript.runtime.values.FunctionValue; |
6 | 10 | import me.topchetoeu.jscript.runtime.values.NativeFunction; |
| 11 | +import me.topchetoeu.jscript.runtime.values.Values; |
7 | 12 | import me.topchetoeu.jscript.utils.interop.Arguments; |
8 | 13 | import me.topchetoeu.jscript.utils.interop.Expose; |
| 14 | +import me.topchetoeu.jscript.utils.interop.ExposeConstructor; |
9 | 15 | import me.topchetoeu.jscript.utils.interop.ExposeTarget; |
10 | 16 | import me.topchetoeu.jscript.utils.interop.WrapperName; |
11 | 17 |
|
12 | 18 | @WrapperName("Function") |
13 | 19 | public class FunctionLib { |
| 20 | + private static int i; |
| 21 | + |
14 | 22 | @Expose public static Object __apply(Arguments args) { |
15 | 23 | return args.self(FunctionValue.class).call(args.ctx, args.get(0), args.convert(1, ArrayValue.class).toArray()); |
16 | 24 | } |
@@ -51,4 +59,25 @@ public static FunctionValue __asyncGenerator(Arguments args) { |
51 | 59 | public static FunctionValue __generator(Arguments args) { |
52 | 60 | return new GeneratorFunctionLib(args.convert(0, CodeFunction.class)); |
53 | 61 | } |
| 62 | + |
| 63 | + @ExposeConstructor |
| 64 | + public static Object __constructor(Arguments args) { |
| 65 | + var compiler = Compiler.get(args); |
| 66 | + |
| 67 | + var parts = args.convert(String.class); |
| 68 | + if (parts.length == 0) parts = new String[] { "" }; |
| 69 | + |
| 70 | + var src = "return function("; |
| 71 | + |
| 72 | + for (var i = 0; i < parts.length - 1; i++) { |
| 73 | + if (i != 0) src += ","; |
| 74 | + src += parts[i]; |
| 75 | + } |
| 76 | + |
| 77 | + src += "){" + parts[parts.length - 1] + "}"; |
| 78 | + |
| 79 | + var body = compiler.compile(new Filename("jscript", "func/" + i++), src); |
| 80 | + var func = new CodeFunction(Context.clean(args.ctx), "", body, new ValueVariable[0]); |
| 81 | + return Values.call(args, func, null); |
| 82 | + } |
54 | 83 | } |
0 commit comments