Skip to content

Commit db80d26

Browse files
committed
fix: grower-rsに合わせてBool, Charに対応
1 parent 5887019 commit db80d26

File tree

1 file changed

+45
-36
lines changed

1 file changed

+45
-36
lines changed

grower-js/src/jsni.ts

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ type _Window = Window & typeof global & {
1010
[FUNCTIONS]: Record<string, JSNIFunction>;
1111
};
1212

13+
enum JSNIKind {
14+
I8 = 0,
15+
I16 = 1,
16+
I32 = 2,
17+
I64 = 3,
18+
U8 = 4,
19+
U16 = 5,
20+
U32 = 6,
21+
U64 = 7,
22+
F32 = 8,
23+
F64 = 9,
24+
Bool = 10,
25+
Char = 11,
26+
String = 12,
27+
VecU8 = 13,
28+
Null = 14,
29+
}
30+
1331
export default class JavaScriptNativeInterface {
1432

1533
private readonly imports: GrowerRsImports;
@@ -27,7 +45,6 @@ export default class JavaScriptNativeInterface {
2745

2846
(window as _Window)[ENTRY_POINT] = async (jsFuncNamePtr: number, argsPtr: number, argsCount: number): Promise<number> => {
2947
const session = new JSNIFunctionCallingSession(this.imports, this.memory, jsFuncNamePtr, argsPtr, argsCount);
30-
console.log(session);
3148
const value = await session.call();
3249
return value ? session.buildReturnValues(value) : -1;
3350
};
@@ -70,7 +87,6 @@ class JSNIFunctionCallingSession {
7087
}
7188

7289
async call() {
73-
console.log(`JSNI: Calling function ${this.functionName} with args`, this.args, (window as _Window)[FUNCTIONS][this.functionName]);
7490
return (window as _Window)[FUNCTIONS][this.functionName](...this.args);
7591
}
7692

@@ -79,66 +95,59 @@ class JSNIFunctionCallingSession {
7995
for (let i = 0; i < this.argsCount; i++) {
8096
const type = this.view.getUint8(this.argsPtr + i * 16 + 8);
8197
switch (type) {
82-
// i8
83-
case 0:
98+
case JSNIKind.I8:
8499
args.push(this.view.getInt8(this.argsPtr + i * 16));
85100
break;
86-
// i16
87-
case 1:
101+
case JSNIKind.I16:
88102
args.push(this.view.getInt16(this.argsPtr + i * 16, true));
89103
break;
90-
// i32
91-
case 2:
104+
case JSNIKind.I32:
92105
args.push(this.view.getInt32(this.argsPtr + i * 16, true));
93106
break;
94-
// i64
95-
case 3:
107+
case JSNIKind.I64:
96108
args.push(this.view.getBigInt64(this.argsPtr + i * 16, true));
97109
break;
98-
// u8
99-
case 4:
110+
case JSNIKind.U8:
100111
args.push(this.view.getUint8(this.argsPtr + i * 16));
101112
break;
102-
// u16
103-
case 5:
113+
case JSNIKind.U16:
104114
args.push(this.view.getUint16(this.argsPtr + i * 16, true));
105115
break;
106-
// u32
107-
case 6:
116+
case JSNIKind.U32:
108117
args.push(this.view.getUint32(this.argsPtr + i * 16, true));
109118
break;
110-
// u64
111-
case 7:
119+
case JSNIKind.U64:
112120
args.push(this.view.getBigUint64(this.argsPtr + i * 16, true));
113121
break;
114-
// f32
115-
case 8:
122+
case JSNIKind.F64:
123+
args.push(this.view.getFloat64(this.argsPtr + i * 16, true));
124+
break;
125+
case JSNIKind.F32:
116126
args.push(this.view.getFloat32(this.argsPtr + i * 16, true));
117127
break;
118-
// f64
119-
case 9:
120-
args.push(this.view.getFloat64(this.argsPtr + i * 16, true));
128+
case JSNIKind.Bool:
129+
args.push(this.view.getUint8(this.argsPtr + i * 16) !== 0);
130+
break;
131+
case JSNIKind.Char:
132+
args.push(String.fromCharCode(this.view.getUint16(this.argsPtr + i * 16, true)));
121133
break;
122-
// String
123-
case 10:
134+
case JSNIKind.String:
124135
args.push(
125136
this.readString(
126137
this.view.getUint32(this.argsPtr + i * 16, true),
127138
this.view.getUint32(this.argsPtr + i * 16 + 4, true)
128139
)
129140
);
130141
break;
131-
// U8Array
132-
case 11:
142+
case JSNIKind.VecU8:
133143
args.push(
134144
this.readVec(
135145
this.view.getUint32(this.argsPtr + i * 16, true),
136146
this.view.getUint32(this.argsPtr + i * 16 + 4, true)
137147
)
138148
);
139149
break;
140-
// null
141-
case 13:
150+
case JSNIKind.Null:
142151
args.push(null);
143152
break;
144153
}
@@ -156,15 +165,15 @@ class JSNIFunctionCallingSession {
156165
case "number":
157166
if (Number.isInteger(values[i])) {
158167
this.view.setBigInt64(ptr + i * 16, BigInt(values[i]), true);
159-
this.view.setInt32(ptr + i * 16 + 8, 3, true);
168+
this.view.setInt32(ptr + i * 16 + 8, JSNIKind.I64, true);
160169
} else {
161170
this.view.setFloat64(ptr + i * 16, values[i], true);
162-
this.view.setInt32(ptr + i * 16 + 8, 9, true);
171+
this.view.setInt32(ptr + i * 16 + 8, JSNIKind.F64, true);
163172
}
164173
break;
165174
case "bigint":
166175
this.view.setBigInt64(ptr + i * 16, values[i], true);
167-
this.view.setInt32(ptr + i * 16 + 8, 3, true);
176+
this.view.setInt32(ptr + i * 16 + 8, JSNIKind.I64, true);
168177
break;
169178
case "string":
170179
const strBytes = new TextEncoder().encode(values[i]);
@@ -173,7 +182,7 @@ class JSNIFunctionCallingSession {
173182
this.arr.set(strBytes, strPtr);
174183
this.arr[strPtr + strBytes.length] = 0; // null terminator
175184
this.view.setUint32(ptr + i * 16, Number(strFatPtr >> BigInt(32)), true);
176-
this.view.setInt32(ptr + i * 16 + 8, 10, true);
185+
this.view.setInt32(ptr + i * 16 + 8, JSNIKind.String, true);
177186
break;
178187
case "object":
179188
if (values[i] instanceof Uint8Array) {
@@ -182,10 +191,10 @@ class JSNIFunctionCallingSession {
182191
const vecPtr = Number(vecFatPtr & BigInt(0xffffffff));
183192
this.arr.set(vecBytes, vecPtr);
184193
this.view.setUint32(ptr + i * 16, Number(vecFatPtr >> BigInt(32)), true);
185-
this.view.setInt32(ptr + i * 16 + 8, 11, true);
194+
this.view.setInt32(ptr + i * 16 + 8, JSNIKind.VecU8, true);
186195
} else if (values[i] === null) {
187-
this.view.setUint32(ptr + i * 16, 0, true);
188-
this.view.setInt32(ptr + i * 16 + 8, 13, true);
196+
// this.view.setUint32(ptr + i * 16, 0, true);
197+
this.view.setInt32(ptr + i * 16 + 8, JSNIKind.Null, true);
189198
} else {
190199
throw new Error(`Unsupported object type: ${typeof values[i]}`);
191200
}

0 commit comments

Comments
 (0)