7
7
addToLibrary ( {
8
8
// This gives correct answers for everything less than 2^{14} = 16384
9
9
// I hope nobody is contemplating functions with 16384 arguments...
10
- $uleb128Encode : ( n , target ) => {
10
+ $uleb128EncodeWithLen__internal : true ,
11
+ $uleb128EncodeWithLen : ( arr ) => {
12
+ const n = arr . length ;
11
13
#if ASSERTIONS
12
14
assert ( n < 16384 ) ;
13
15
#endif
14
- if ( n < 128 ) {
15
- target . push ( n ) ;
16
- } else {
17
- target . push ( ( n % 128 ) | 128 , n >> 7 ) ;
18
- }
16
+ // Note: this LEB128 length encoding produces extra byte for n < 128,
17
+ // but we don't care as it's only used in a temporary representation.
18
+ return [ ( n % 128 ) | 128 , n >> 7 , ...arr ] ;
19
19
} ,
20
20
#if WASM_JS_TYPES
21
21
// Converts a signature like 'vii' into a description of the wasm types, like
22
22
// { parameters: ['i32', 'i32'], results: [] }.
23
+ $sigToWasmTypes__internal : true ,
23
24
$sigToWasmTypes : ( sig ) => {
24
25
#if ASSERTIONS && ! WASM_BIGINT
25
26
assert ( ! sig . includes ( 'j' ) , 'i64 not permitted in function signatures when WASM_BIGINT is disabled' ) ;
@@ -49,49 +50,41 @@ addToLibrary({
49
50
return type ;
50
51
} ,
51
52
#endif
52
- $generateFuncType__deps : [ '$uleb128Encode' ] ,
53
- $generateFuncType : ( sig , target ) => {
54
- var sigRet = sig . slice ( 0 , 1 ) ;
55
- var sigParam = sig . slice ( 1 ) ;
56
- var typeCodes = {
57
- 'i' : 0x7f , // i32
53
+ $wasmTypeCodes__internal : true ,
54
+ // Note: using template literal here instead of plain object
55
+ // because jsify serializes objects w/o quotes and Closure will then
56
+ // incorrectly mangle the properties.
57
+ $wasmTypeCodes : ` {
58
+ 'i': 0x7f, // i32
58
59
#if MEMORY64
59
- 'p' : 0x7e , // i64
60
+ 'p': 0x7e, // i64
60
61
#else
61
- 'p' : 0x7f , // i32
62
+ 'p': 0x7f, // i32
62
63
#endif
63
- 'j' : 0x7e , // i64
64
- 'f' : 0x7d , // f32
65
- 'd' : 0x7c , // f64
66
- 'e' : 0x6f , // externref
67
- } ;
68
-
69
- // Parameters, length + signatures
70
- target . push ( 0x60 /* form: func */ ) ;
71
- uleb128Encode ( sigParam . length , target ) ;
72
- for ( var paramType of sigParam ) {
64
+ 'j': 0x7e, // i64
65
+ 'f': 0x7d, // f32
66
+ 'd': 0x7c, // f64
67
+ 'e': 0x6f, // externref
68
+ }` ,
69
+
70
+ $generateTypePack__internal : true ,
71
+ $generateTypePack__deps : [ '$uleb128EncodeWithLen' , '$wasmTypeCodes' ] ,
72
+ $generateTypePack : ( types ) => uleb128EncodeWithLen ( Array . from ( types , ( type ) => {
73
+ var code = wasmTypeCodes [ type ] ;
73
74
#if ASSERTIONS
74
- assert ( paramType in typeCodes , `invalid signature char: ${ paramType } ` ) ;
75
+ assert ( code , `invalid signature char: ${ type } ` ) ;
75
76
#endif
76
- target . push ( typeCodes [ paramType ] ) ;
77
- }
77
+ return code ;
78
+ } ) ) ,
78
79
79
- // Return values, length + signatures
80
- // With no multi-return in MVP, either 0 (void) or 1 (anything else)
81
- if ( sigRet == 'v' ) {
82
- target . push ( 0x00 ) ;
83
- } else {
84
- target . push ( 0x01 , typeCodes [ sigRet ] ) ;
85
- }
86
- } ,
87
80
// Wraps a JS function as a wasm function with a given signature.
88
81
#if ! WASM2JS
89
82
$convertJsFunctionToWasm__deps : [
90
- '$uleb128Encode ' ,
83
+ '$uleb128EncodeWithLen ' ,
91
84
#if WASM_JS_TYPES
92
85
'$sigToWasmTypes' ,
93
86
#endif
94
- '$generateFuncType '
87
+ '$generateTypePack '
95
88
] ,
96
89
#endif
97
90
$convertJsFunctionToWasm : ( func , sig ) => {
@@ -111,25 +104,23 @@ addToLibrary({
111
104
return new WebAssembly . Function ( sigToWasmTypes ( sig ) , func ) ;
112
105
}
113
106
#endif
114
- // The module is static, with the exception of the type section, which is
115
- // generated based on the signature passed in.
116
- var typeSectionBody = [
117
- 0x01 , // count: 1
118
- ] ;
119
- generateFuncType ( sig , typeSectionBody ) ;
120
107
121
108
// Rest of the module is static
122
- var bytes = [
109
+ var bytes = Uint8Array . of (
123
110
0x00 , 0x61 , 0x73 , 0x6d , // magic ("\0asm")
124
111
0x01 , 0x00 , 0x00 , 0x00 , // version: 1
125
112
0x01 , // Type section code
126
- ] ;
127
- // Write the overall length of the type section followed by the body
128
- uleb128Encode ( typeSectionBody . length , bytes ) ;
129
- bytes . push ( ...typeSectionBody ) ;
130
-
131
- // The rest of the module is static
132
- bytes . push (
113
+ // The module is static, with the exception of the type section, which is
114
+ // generated based on the signature passed in.
115
+ ...uleb128EncodeWithLen ( [
116
+ 0x01 , // count: 1
117
+ 0x60 /* form: func */ ,
118
+ // param types
119
+ ...generateTypePack ( sig . slice ( 1 ) ) ,
120
+ // return types (for now only supporting [] if `void` and single [T] otherwise)
121
+ ...generateTypePack ( sig [ 0 ] === 'v' ? '' : sig [ 0 ] )
122
+ ] ) ,
123
+ // The rest of the module is static
133
124
0x02 , 0x07 , // import section
134
125
// (import "e" "f" (func 0 (type 0)))
135
126
0x01 , 0x01 , 0x65 , 0x01 , 0x66 , 0x00 , 0x00 ,
@@ -140,7 +131,7 @@ addToLibrary({
140
131
141
132
// We can compile this wasm module synchronously because it is very small.
142
133
// This accepts an import (at "e.f"), that it reroutes to an export (at "f")
143
- var module = new WebAssembly . Module ( new Uint8Array ( bytes ) ) ;
134
+ var module = new WebAssembly . Module ( bytes ) ;
144
135
var instance = new WebAssembly . Instance ( module , { 'e' : { 'f' : func } } ) ;
145
136
var wrappedFunc = instance . exports [ 'f' ] ;
146
137
return wrappedFunc ;
@@ -158,17 +149,19 @@ addToLibrary({
158
149
if ( freeTableIndexes . length ) {
159
150
return freeTableIndexes . pop ( ) ;
160
151
}
161
- // Grow the table
152
+ #if ASSERTIONS
162
153
try {
163
- /** @suppress {checkTypes} */
164
- wasmTable . grow ( { { { toIndexType ( '1' ) } } } ) ;
154
+ #endif
155
+ // Grow the table
156
+ return wasmTable [ 'grow' ] ( { { { toIndexType ( '1' ) } } } ) ;
157
+ #if ASSERTIONS
165
158
} catch ( err ) {
166
159
if ( ! ( err instanceof RangeError ) ) {
167
160
throw err ;
168
161
}
169
162
throw 'Unable to grow wasm table. Set ALLOW_TABLE_GROWTH.' ;
170
163
}
171
- return { { { from64Expr ( 'wasmTable.length' ) } } } - 1 ;
164
+ #endif
172
165
} ,
173
166
174
167
$updateTableMap__deps : [ '$getWasmTableEntry' ] ,
0 commit comments