Skip to content

Commit 0323f8f

Browse files
WIP
1 parent 6c19dd2 commit 0323f8f

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

Diff for: IntegrationTests/JavaScriptKitExec/Sources/JavaScriptKitExec/main.swift

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ Value_Construction: do {
7171
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
7272
let globalObject1Ref = try expectObject(globalObject1)
7373
let prop_7 = getJSValue(this: globalObject1Ref, name: "prop_7")
74-
print(prop_7)
7574
try expectEqual(Double.construct(from: prop_7), 3.14)
7675
try expectEqual(Float.construct(from: prop_7), 3.14)
7776
} catch {

Diff for: IntegrationTests/index.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ const readFile = promisify(fs.readFile);
99
const swift = new SwiftRuntime();
1010
// Instantiate a new WASI Instance
1111
const wasmFs = new WasmFs();
12+
// Output stdout and stderr to console
13+
const originalWriteSync = wasmFs.fs.writeSync;
14+
wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {
15+
const text = new TextDecoder("utf-8").decode(buffer);
16+
switch (fd) {
17+
case 1:
18+
console.log(text);
19+
break;
20+
case 2:
21+
console.error(text);
22+
break;
23+
}
24+
return originalWriteSync(fd, buffer, offset, length, position);
25+
};
1226
let wasi = new WASI({
1327
args: [],
1428
env: {},
@@ -39,7 +53,6 @@ global.globalObject1 = {
3953
},
4054
"prop_6": {
4155
"call_host_1": () => {
42-
console.log(global.globalObject1.prop_6)
4356
return global.globalObject1.prop_6.host_func_1()
4457
}
4558
},
@@ -71,10 +84,6 @@ const startWasiTask = async () => {
7184
swift.setInsance(instance);
7285
// Start the WebAssembly WASI instance!
7386
wasi.start(instance);
74-
75-
// Output what's inside of /dev/stdout!
76-
const stdout = await wasmFs.getStdOut();
77-
console.log(stdout);
7887
};
7988
startWasiTask().catch(err => {
8089
console.log(err)

Diff for: Runtime/src/index.ts

+8-18
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ export class SwiftRuntime {
102102
const argv = exports.swjs_prepare_host_function_call(argc)
103103
for (let index = 0; index < args.length; index++) {
104104
const argument = args[index]
105-
const base = argv + 20 * index
106-
writeValue(argument, base, base + 4, base + 8, base + 12)
105+
const base = argv + 24 * index
106+
writeValue(argument, base, base + 4, base + 8, base + 16)
107107
}
108108
let output: any;
109109
const callback_func_ref = this.heap.allocHeap(function (result: any) {
@@ -184,10 +184,6 @@ export class SwiftRuntime {
184184
return undefined
185185
}
186186
case JavaScriptValueKind.Function: {
187-
// console.log("decoding function:")
188-
// console.log(" payload1: " + payload1)
189-
// console.log("Heap Contents:")
190-
// console.log(this.heap)
191187
return this.heap.referenceHeap(payload1)
192188
}
193189
default:
@@ -203,6 +199,7 @@ export class SwiftRuntime {
203199
writeUint32(kind_ptr, JavaScriptValueKind.Null);
204200
writeUint32(payload1_ptr, 0);
205201
writeUint32(payload2_ptr, 0);
202+
return;
206203
}
207204
switch (typeof value) {
208205
case "boolean": {
@@ -250,16 +247,16 @@ export class SwiftRuntime {
250247
}
251248

252249
// Note:
253-
// `decodeValues` assumes that the size of RawJSValue is 16
254-
// and the alignment of it is 4
250+
// `decodeValues` assumes that the size of RawJSValue is 24
251+
// and the alignment of it is 8
255252
const decodeValues = (ptr: pointer, length: number) => {
256253
let result = []
257254
for (let index = 0; index < length; index++) {
258-
const base = ptr + 16 * index
255+
const base = ptr + 24 * index
259256
const kind = readUInt32(base)
260-
const payload1 = readFloat64(base + 4)
257+
const payload1 = readUInt32(base + 4)
261258
const payload2 = readUInt32(base + 8)
262-
const payload3 = readUInt32(base + 12)
259+
const payload3 = readFloat64(base + 16)
263260
result.push(decodeValue(kind, payload1, payload2, payload3))
264261
}
265262
return result
@@ -272,13 +269,6 @@ export class SwiftRuntime {
272269
payload1: number, payload2: number, payload3: number
273270
) => {
274271
const obj = this.heap.referenceHeap(ref);
275-
// console.log("swjs_set_prop");
276-
// console.log(" name: " + readString(name, length));
277-
// console.log(" kind: " + kind)
278-
// console.log(" payload1: " + payload1)
279-
// console.log(" payload2: " + payload2)
280-
// console.log(" payload3: " + payload3)
281-
// console.log(" value: " + decodeValue(kind, payload1, payload2, payload3))
282272
Reflect.set(obj, readString(name, length), decodeValue(kind, payload1, payload2, payload3))
283273
},
284274
swjs_get_prop: (

Diff for: Sources/JavaScriptKit/JSFunction.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ public class JSFunctionRef: JSObjectRef {
55

66
@discardableResult
77
public func dynamicallyCall(withArguments arguments: [JSValueConvertible]) -> JSValue {
8-
let result = arguments.withRawJSValues { rawValues in
9-
rawValues.withUnsafeBufferPointer { bufferPointer -> RawJSValue in
8+
let result = arguments.withRawJSValues { rawValues -> RawJSValue in
9+
return rawValues.withUnsafeBufferPointer { bufferPointer -> RawJSValue in
1010
let argv = bufferPointer.baseAddress
1111
let argc = bufferPointer.count
1212
var result = RawJSValue()

0 commit comments

Comments
 (0)