@@ -97,8 +97,35 @@ let kWasmI64 = 0x7e;
97
97
let kWasmF32 = 0x7d ;
98
98
let kWasmF64 = 0x7c ;
99
99
let kWasmS128 = 0x7b ;
100
- let kWasmAnyRef = 0x6f ;
101
- let kWasmAnyFunc = 0x70 ;
100
+
101
+ // These are defined as negative integers to distinguish them from positive type
102
+ // indices.
103
+ let kWasmNullFuncRef = - 0x0d ;
104
+ let kWasmNullExternRef = - 0x0e ;
105
+ let kWasmNullRef = - 0x0f ;
106
+ let kWasmFuncRef = - 0x10 ;
107
+ let kWasmAnyFunc = kWasmFuncRef ; // Alias named as in the JS API spec
108
+ let kWasmExternRef = - 0x11 ;
109
+ let kWasmAnyRef = - 0x12 ;
110
+
111
+ // Use the positive-byte versions inside function bodies.
112
+ let kLeb128Mask = 0x7f ;
113
+ let kFuncRefCode = kWasmFuncRef & kLeb128Mask ;
114
+ let kAnyFuncCode = kFuncRefCode ; // Alias named as in the JS API spec
115
+ let kExternRefCode = kWasmExternRef & kLeb128Mask ;
116
+ let kAnyRefCode = kWasmAnyRef & kLeb128Mask ;
117
+ let kNullExternRefCode = kWasmNullExternRef & kLeb128Mask ;
118
+ let kNullFuncRefCode = kWasmNullFuncRef & kLeb128Mask ;
119
+ let kNullRefCode = kWasmNullRef & kLeb128Mask ;
120
+
121
+ let kWasmRefNull = 0x63 ;
122
+ let kWasmRef = 0x64 ;
123
+ function wasmRefNullType ( heap_type , is_shared = false ) {
124
+ return { opcode : kWasmRefNull , heap_type : heap_type , is_shared : is_shared } ;
125
+ }
126
+ function wasmRefType ( heap_type , is_shared = false ) {
127
+ return { opcode : kWasmRef , heap_type : heap_type , is_shared : is_shared } ;
128
+ }
102
129
103
130
let kExternalFunction = 0 ;
104
131
let kExternalTable = 1 ;
@@ -146,14 +173,14 @@ let kSig_v_f = makeSig([kWasmF32], []);
146
173
let kSig_f_f = makeSig ( [ kWasmF32 ] , [ kWasmF32 ] ) ;
147
174
let kSig_f_d = makeSig ( [ kWasmF64 ] , [ kWasmF32 ] ) ;
148
175
let kSig_d_d = makeSig ( [ kWasmF64 ] , [ kWasmF64 ] ) ;
149
- let kSig_r_r = makeSig ( [ kWasmAnyRef ] , [ kWasmAnyRef ] ) ;
176
+ let kSig_r_r = makeSig ( [ kWasmExternRef ] , [ kWasmExternRef ] ) ;
150
177
let kSig_a_a = makeSig ( [ kWasmAnyFunc ] , [ kWasmAnyFunc ] ) ;
151
- let kSig_i_r = makeSig ( [ kWasmAnyRef ] , [ kWasmI32 ] ) ;
152
- let kSig_v_r = makeSig ( [ kWasmAnyRef ] , [ ] ) ;
178
+ let kSig_i_r = makeSig ( [ kWasmExternRef ] , [ kWasmI32 ] ) ;
179
+ let kSig_v_r = makeSig ( [ kWasmExternRef ] , [ ] ) ;
153
180
let kSig_v_a = makeSig ( [ kWasmAnyFunc ] , [ ] ) ;
154
- let kSig_v_rr = makeSig ( [ kWasmAnyRef , kWasmAnyRef ] , [ ] ) ;
181
+ let kSig_v_rr = makeSig ( [ kWasmExternRef , kWasmExternRef ] , [ ] ) ;
155
182
let kSig_v_aa = makeSig ( [ kWasmAnyFunc , kWasmAnyFunc ] , [ ] ) ;
156
- let kSig_r_v = makeSig ( [ ] , [ kWasmAnyRef ] ) ;
183
+ let kSig_r_v = makeSig ( [ ] , [ kWasmExternRef ] ) ;
157
184
let kSig_a_v = makeSig ( [ ] , [ kWasmAnyFunc ] ) ;
158
185
let kSig_a_i = makeSig ( [ kWasmI32 ] , [ kWasmAnyFunc ] ) ;
159
186
@@ -554,6 +581,16 @@ class Binary {
554
581
}
555
582
}
556
583
584
+ emit_type ( type ) {
585
+ if ( ( typeof type ) == 'number' ) {
586
+ this . emit_u8 ( type >= 0 ? type : type & kLeb128Mask ) ;
587
+ } else {
588
+ this . emit_u8 ( type . opcode ) ;
589
+ if ( 'depth' in type ) this . emit_u8 ( type . depth ) ;
590
+ this . emit_heap_type ( type . heap_type ) ;
591
+ }
592
+ }
593
+
557
594
emit_header ( ) {
558
595
this . emit_bytes ( [
559
596
kWasmH0 , kWasmH1 , kWasmH2 , kWasmH3 , kWasmV0 , kWasmV1 , kWasmV2 , kWasmV3
@@ -898,11 +935,11 @@ class WasmModuleBuilder {
898
935
section . emit_u8 ( kWasmFunctionTypeForm ) ;
899
936
section . emit_u32v ( type . params . length ) ;
900
937
for ( let param of type . params ) {
901
- section . emit_u8 ( param ) ;
938
+ section . emit_type ( param ) ;
902
939
}
903
940
section . emit_u32v ( type . results . length ) ;
904
941
for ( let result of type . results ) {
905
- section . emit_u8 ( result ) ;
942
+ section . emit_type ( result ) ;
906
943
}
907
944
}
908
945
} ) ;
@@ -1161,7 +1198,7 @@ class WasmModuleBuilder {
1161
1198
local_decls . push ( { count : l . s128_count , type : kWasmS128 } ) ;
1162
1199
}
1163
1200
if ( l . anyref_count > 0 ) {
1164
- local_decls . push ( { count : l . anyref_count , type : kWasmAnyRef } ) ;
1201
+ local_decls . push ( { count : l . anyref_count , type : kWasmExternRef } ) ;
1165
1202
}
1166
1203
if ( l . anyfunc_count > 0 ) {
1167
1204
local_decls . push ( { count : l . anyfunc_count , type : kWasmAnyFunc } ) ;
0 commit comments