@@ -10,6 +10,24 @@ type _Window = Window & typeof global & {
10
10
[ FUNCTIONS ] : Record < string , JSNIFunction > ;
11
11
} ;
12
12
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
+
13
31
export default class JavaScriptNativeInterface {
14
32
15
33
private readonly imports : GrowerRsImports ;
@@ -27,7 +45,6 @@ export default class JavaScriptNativeInterface {
27
45
28
46
( window as _Window ) [ ENTRY_POINT ] = async ( jsFuncNamePtr : number , argsPtr : number , argsCount : number ) : Promise < number > => {
29
47
const session = new JSNIFunctionCallingSession ( this . imports , this . memory , jsFuncNamePtr , argsPtr , argsCount ) ;
30
- console . log ( session ) ;
31
48
const value = await session . call ( ) ;
32
49
return value ? session . buildReturnValues ( value ) : - 1 ;
33
50
} ;
@@ -70,7 +87,6 @@ class JSNIFunctionCallingSession {
70
87
}
71
88
72
89
async call ( ) {
73
- console . log ( `JSNI: Calling function ${ this . functionName } with args` , this . args , ( window as _Window ) [ FUNCTIONS ] [ this . functionName ] ) ;
74
90
return ( window as _Window ) [ FUNCTIONS ] [ this . functionName ] ( ...this . args ) ;
75
91
}
76
92
@@ -79,66 +95,59 @@ class JSNIFunctionCallingSession {
79
95
for ( let i = 0 ; i < this . argsCount ; i ++ ) {
80
96
const type = this . view . getUint8 ( this . argsPtr + i * 16 + 8 ) ;
81
97
switch ( type ) {
82
- // i8
83
- case 0 :
98
+ case JSNIKind . I8 :
84
99
args . push ( this . view . getInt8 ( this . argsPtr + i * 16 ) ) ;
85
100
break ;
86
- // i16
87
- case 1 :
101
+ case JSNIKind . I16 :
88
102
args . push ( this . view . getInt16 ( this . argsPtr + i * 16 , true ) ) ;
89
103
break ;
90
- // i32
91
- case 2 :
104
+ case JSNIKind . I32 :
92
105
args . push ( this . view . getInt32 ( this . argsPtr + i * 16 , true ) ) ;
93
106
break ;
94
- // i64
95
- case 3 :
107
+ case JSNIKind . I64 :
96
108
args . push ( this . view . getBigInt64 ( this . argsPtr + i * 16 , true ) ) ;
97
109
break ;
98
- // u8
99
- case 4 :
110
+ case JSNIKind . U8 :
100
111
args . push ( this . view . getUint8 ( this . argsPtr + i * 16 ) ) ;
101
112
break ;
102
- // u16
103
- case 5 :
113
+ case JSNIKind . U16 :
104
114
args . push ( this . view . getUint16 ( this . argsPtr + i * 16 , true ) ) ;
105
115
break ;
106
- // u32
107
- case 6 :
116
+ case JSNIKind . U32 :
108
117
args . push ( this . view . getUint32 ( this . argsPtr + i * 16 , true ) ) ;
109
118
break ;
110
- // u64
111
- case 7 :
119
+ case JSNIKind . U64 :
112
120
args . push ( this . view . getBigUint64 ( this . argsPtr + i * 16 , true ) ) ;
113
121
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 :
116
126
args . push ( this . view . getFloat32 ( this . argsPtr + i * 16 , true ) ) ;
117
127
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 ) ) ) ;
121
133
break ;
122
- // String
123
- case 10 :
134
+ case JSNIKind . String :
124
135
args . push (
125
136
this . readString (
126
137
this . view . getUint32 ( this . argsPtr + i * 16 , true ) ,
127
138
this . view . getUint32 ( this . argsPtr + i * 16 + 4 , true )
128
139
)
129
140
) ;
130
141
break ;
131
- // U8Array
132
- case 11 :
142
+ case JSNIKind . VecU8 :
133
143
args . push (
134
144
this . readVec (
135
145
this . view . getUint32 ( this . argsPtr + i * 16 , true ) ,
136
146
this . view . getUint32 ( this . argsPtr + i * 16 + 4 , true )
137
147
)
138
148
) ;
139
149
break ;
140
- // null
141
- case 13 :
150
+ case JSNIKind . Null :
142
151
args . push ( null ) ;
143
152
break ;
144
153
}
@@ -156,15 +165,15 @@ class JSNIFunctionCallingSession {
156
165
case "number" :
157
166
if ( Number . isInteger ( values [ i ] ) ) {
158
167
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 ) ;
160
169
} else {
161
170
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 ) ;
163
172
}
164
173
break ;
165
174
case "bigint" :
166
175
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 ) ;
168
177
break ;
169
178
case "string" :
170
179
const strBytes = new TextEncoder ( ) . encode ( values [ i ] ) ;
@@ -173,7 +182,7 @@ class JSNIFunctionCallingSession {
173
182
this . arr . set ( strBytes , strPtr ) ;
174
183
this . arr [ strPtr + strBytes . length ] = 0 ; // null terminator
175
184
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 ) ;
177
186
break ;
178
187
case "object" :
179
188
if ( values [ i ] instanceof Uint8Array ) {
@@ -182,10 +191,10 @@ class JSNIFunctionCallingSession {
182
191
const vecPtr = Number ( vecFatPtr & BigInt ( 0xffffffff ) ) ;
183
192
this . arr . set ( vecBytes , vecPtr ) ;
184
193
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 ) ;
186
195
} 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 ) ;
189
198
} else {
190
199
throw new Error ( `Unsupported object type: ${ typeof values [ i ] } ` ) ;
191
200
}
0 commit comments