7
7
mergeInto ( LibraryManager . library , {
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 : function ( n ) {
10
+ $uleb128Encode : function ( n , target ) {
11
11
#if ASSERTIONS
12
12
assert ( n < 16384 ) ;
13
13
#endif
14
14
if ( n < 128 ) {
15
- return [ n ] ;
15
+ target . push ( n ) ;
16
+ } else {
17
+ target . push ( ( n % 128 ) | 128 , n >> 7 ) ;
16
18
}
17
- return [ ( n % 128 ) | 128 , n >> 7 ] ;
18
19
} ,
19
20
20
21
// Converts a signature like 'vii' into a description of the wasm types, like
@@ -61,7 +62,7 @@ mergeInto(LibraryManager.library, {
61
62
62
63
// The module is static, with the exception of the type section, which is
63
64
// generated based on the signature passed in.
64
- var typeSection = [
65
+ var typeSectionBody = [
65
66
0x01 , // count: 1
66
67
0x60 , // form: func
67
68
] ;
@@ -80,50 +81,46 @@ mergeInto(LibraryManager.library, {
80
81
} ;
81
82
82
83
// Parameters, length + signatures
83
- typeSection = typeSection . concat ( uleb128Encode ( sigParam . length ) ) ;
84
+ uleb128Encode ( sigParam . length , typeSectionBody ) ;
84
85
for ( var i = 0 ; i < sigParam . length ; ++ i ) {
85
86
#if ASSERTIONS
86
87
assert ( sigParam [ i ] in typeCodes , 'invalid signature char: ' + sigParam [ i ] ) ;
87
88
#endif
88
- typeSection . push ( typeCodes [ sigParam [ i ] ] ) ;
89
+ typeSectionBody . push ( typeCodes [ sigParam [ i ] ] ) ;
89
90
}
90
91
91
92
// Return values, length + signatures
92
93
// With no multi-return in MVP, either 0 (void) or 1 (anything else)
93
94
if ( sigRet == 'v' ) {
94
- typeSection . push ( 0x00 ) ;
95
+ typeSectionBody . push ( 0x00 ) ;
95
96
} else {
96
- typeSection = typeSection . concat ( [ 0x01 , typeCodes [ sigRet ] ] ) ;
97
+ typeSectionBody . push ( 0x01 , typeCodes [ sigRet ] ) ;
97
98
}
98
99
99
- // Write the section code and overall length of the type section into the
100
- // section header
101
- typeSection = [ 0x01 /* Type section code */ ] . concat (
102
- uleb128Encode ( typeSection . length ) ,
103
- typeSection
104
- ) ;
105
-
106
100
// Rest of the module is static
107
- var bytes = new Uint8Array ( [
101
+ var bytes = [
108
102
0x00 , 0x61 , 0x73 , 0x6d , // magic ("\0asm")
109
103
0x01 , 0x00 , 0x00 , 0x00 , // version: 1
110
- ] . concat ( typeSection , [
104
+ 0x01 , // Type section code
105
+ ] ;
106
+ // Write the overall length of the type section followed by the body
107
+ uleb128Encode ( typeSectionBody . length , bytes ) ;
108
+ bytes . push . apply ( bytes , typeSectionBody ) ;
109
+
110
+ // The rest of the module is static
111
+ bytes . push (
111
112
0x02 , 0x07 , // import section
112
113
// (import "e" "f" (func 0 (type 0)))
113
114
0x01 , 0x01 , 0x65 , 0x01 , 0x66 , 0x00 , 0x00 ,
114
115
0x07 , 0x05 , // export section
115
116
// (export "f" (func 0 (type 0)))
116
117
0x01 , 0x01 , 0x66 , 0x00 , 0x00 ,
117
- ] ) ) ;
118
+ ) ;
118
119
119
- // We can compile this wasm module synchronously because it is very small.
120
+ // We can compile this wasm module synchronously because it is very small.
120
121
// This accepts an import (at "e.f"), that it reroutes to an export (at "f")
121
- var module = new WebAssembly . Module ( bytes ) ;
122
- var instance = new WebAssembly . Instance ( module , {
123
- 'e' : {
124
- 'f' : func
125
- }
126
- } ) ;
122
+ var module = new WebAssembly . Module ( new Uint8Array ( bytes ) ) ;
123
+ var instance = new WebAssembly . Instance ( module , { 'e' : { 'f' : func } } ) ;
127
124
var wrappedFunc = instance . exports [ 'f' ] ;
128
125
return wrappedFunc ;
129
126
#endif // WASM2JS
0 commit comments