Skip to content

Commit 52891cc

Browse files
committed
review
1 parent 3565047 commit 52891cc

File tree

1 file changed

+26
-27
lines changed
  • core/src/main/java/io/roastedroot/quickjs4j/core

1 file changed

+26
-27
lines changed

core/src/main/java/io/roastedroot/quickjs4j/core/Engine.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@WasmModuleInterface(WasmResource.absoluteFile)
3030
public final class Engine implements AutoCloseable {
3131
private static final int ALIGNMENT = 1;
32-
private static final byte[] SEMICOLON_NL = ";\n".getBytes(UTF_8);
32+
3333
private static final byte[] NULL_BYTES = "null".getBytes(UTF_8);
3434
public static final ObjectMapper DEFAULT_OBJECT_MAPPER = new ObjectMapper();
3535

@@ -131,7 +131,7 @@ public Object invokeGuestFunction(
131131
moduleName, name, args, compilePortableGuestFunction(libraryCode));
132132
}
133133

134-
public byte[] invokeFunction() {
134+
private String invokeFunction() {
135135
var funInvoke =
136136
"globalThis[quickjs4j_engine.module_name()][quickjs4j_engine.function_name()](...JSON.parse(quickjs4j_engine.args()))";
137137
Function<String, String> setResult =
@@ -142,36 +142,35 @@ public byte[] invokeFunction() {
142142
+ value
143143
+ "]))";
144144

145-
return ("Promise.resolve("
146-
+ funInvoke
147-
+ ").then((value) => { "
148-
+ setResult.apply("value")
149-
+ " }, (err) => { throw err; })")
150-
.getBytes(UTF_8);
145+
return "Promise.resolve("
146+
+ funInvoke
147+
+ ").then((value) => { "
148+
+ setResult.apply("value")
149+
+ " }, (err) => { throw err; })";
151150
}
152151

153152
// Plan:
154153
// we compile a static version of the module and we invoke it parametrically using a basic
155154
// protocol
156155
// { moduleName: "..", functionName: "..", args: "stringified args" }
157-
public byte[] compilePortableGuestFunction(String libraryCode) {
158-
return compilePortableGuestFunction(libraryCode.getBytes(UTF_8));
156+
public byte[] compilePortableGuestFunction(byte[] libraryCode) {
157+
return compilePortableGuestFunction(new String(libraryCode, UTF_8));
159158
}
160159

161-
public byte[] compilePortableGuestFunction(byte[] libraryCode) {
160+
public byte[] compilePortableGuestFunction(String libraryCode) {
162161
int codePtr = 0;
163162
try {
164-
var buf = new ByteArrayOutputStream();
165-
buf.writeBytes(jsPrelude());
166-
buf.write('\n');
167-
buf.writeBytes(libraryCode);
168-
buf.write('\n');
169-
buf.writeBytes(jsSuffix());
170-
buf.write('\n');
171-
buf.writeBytes(invokeFunction());
172-
buf.writeBytes(SEMICOLON_NL);
173-
174-
codePtr = compileRaw(buf.toByteArray());
163+
var buf = new StringBuilder();
164+
buf.append(jsPrelude());
165+
buf.append('\n');
166+
buf.append(libraryCode);
167+
buf.append('\n');
168+
buf.append(jsSuffix());
169+
buf.append('\n');
170+
buf.append(invokeFunction());
171+
buf.append(";\n");
172+
173+
codePtr = compileRaw(buf.toString().getBytes(UTF_8));
175174
return readCompiled(codePtr);
176175
} finally {
177176
if (codePtr != 0) {
@@ -322,7 +321,7 @@ private long[] invokeBuiltin(Instance instance, long[] args) {
322321
this::invokeBuiltin);
323322

324323
// This function dynamically generates the global functions defined by the Builtins
325-
private byte[] jsPrelude() {
324+
private String jsPrelude() {
326325
var preludeBuilder = new StringBuilder();
327326
for (Map.Entry<String, Builtins> builtin : builtins.entrySet()) {
328327
preludeBuilder.append("globalThis." + builtin.getKey() + " = {};\n");
@@ -339,11 +338,11 @@ private byte[] jsPrelude() {
339338
+ "\", JSON.stringify(args))) };\n");
340339
}
341340
}
342-
return preludeBuilder.toString().getBytes(UTF_8);
341+
return preludeBuilder.toString();
343342
}
344343

345344
// This function dynamically generates the js handlers for Invokables
346-
private byte[] jsSuffix() {
345+
private String jsSuffix() {
347346
var suffixBuilder = new StringBuilder();
348347
for (Map.Entry<String, Invokables> invokable : invokables.entrySet()) {
349348
// The object is already defined by the set_result, just add the handlers
@@ -359,15 +358,15 @@ private byte[] jsSuffix() {
359358
+ ";\n");
360359
}
361360
}
362-
return suffixBuilder.toString().getBytes(UTF_8);
361+
return suffixBuilder.toString();
363362
}
364363

365364
public int compile(String js) {
366365
return compile(js.getBytes(UTF_8));
367366
}
368367

369368
public int compile(byte[] js) {
370-
byte[] prelude = jsPrelude();
369+
byte[] prelude = jsPrelude().getBytes(UTF_8);
371370
byte[] jsCode = new byte[prelude.length + js.length];
372371
System.arraycopy(prelude, 0, jsCode, 0, prelude.length);
373372
System.arraycopy(js, 0, jsCode, prelude.length, js.length);

0 commit comments

Comments
 (0)