@@ -11,6 +11,7 @@ pub mod write;
11
11
pub use read:: Deserialize ;
12
12
pub use write:: Serialize ;
13
13
14
+ #[ cfg( target_family = "wasm" ) ]
14
15
#[ link( wasm_import_module = "shopify_function_v0.0.1" ) ]
15
16
extern "C" {
16
17
// Common API.
@@ -62,6 +63,149 @@ extern "C" {
62
63
fn shopify_function_intern_utf8_str ( context : ContextPtr , ptr : * const u8 , len : usize ) -> usize ;
63
64
}
64
65
66
+ #[ cfg( not( target_family = "wasm" ) ) ]
67
+ mod provider_fallback {
68
+ use super :: { ContextPtr , Val , WriteResult } ;
69
+
70
+ // Read API.
71
+ pub ( crate ) unsafe fn shopify_function_input_get ( context : ContextPtr ) -> Val {
72
+ shopify_function_wasm_api_provider:: read:: shopify_function_input_get ( context)
73
+ }
74
+ pub ( crate ) unsafe fn shopify_function_input_get_val_len (
75
+ context : ContextPtr ,
76
+ scope : Val ,
77
+ ) -> usize {
78
+ shopify_function_wasm_api_provider:: read:: shopify_function_input_get_val_len ( context, scope)
79
+ }
80
+ pub ( crate ) unsafe fn shopify_function_input_read_utf8_str (
81
+ context : ContextPtr ,
82
+ src : usize ,
83
+ out : * mut u8 ,
84
+ len : usize ,
85
+ ) {
86
+ let src =
87
+ shopify_function_wasm_api_provider:: read:: shopify_function_input_get_utf8_str_addr (
88
+ context, src,
89
+ ) ;
90
+ std:: ptr:: copy ( src as _ , out, len) ;
91
+ }
92
+ pub ( crate ) unsafe fn shopify_function_input_get_obj_prop (
93
+ context : ContextPtr ,
94
+ scope : Val ,
95
+ ptr : * const u8 ,
96
+ len : usize ,
97
+ ) -> Val {
98
+ shopify_function_wasm_api_provider:: read:: shopify_function_input_get_obj_prop (
99
+ context, scope, ptr as _ , len,
100
+ )
101
+ }
102
+ pub ( crate ) unsafe fn shopify_function_input_get_interned_obj_prop (
103
+ context : ContextPtr ,
104
+ scope : Val ,
105
+ interned_string_id : shopify_function_wasm_api_core:: InternedStringId ,
106
+ ) -> Val {
107
+ shopify_function_wasm_api_provider:: read:: shopify_function_input_get_interned_obj_prop (
108
+ context,
109
+ scope,
110
+ interned_string_id,
111
+ )
112
+ }
113
+ pub ( crate ) unsafe fn shopify_function_input_get_at_index (
114
+ context : ContextPtr ,
115
+ scope : Val ,
116
+ index : usize ,
117
+ ) -> Val {
118
+ shopify_function_wasm_api_provider:: read:: shopify_function_input_get_at_index (
119
+ context, scope, index,
120
+ )
121
+ }
122
+
123
+ // Write API.
124
+ pub ( crate ) unsafe fn shopify_function_output_new_bool (
125
+ context : ContextPtr ,
126
+ bool : u32 ,
127
+ ) -> WriteResult {
128
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_new_bool ( context, bool)
129
+ }
130
+ pub ( crate ) unsafe fn shopify_function_output_new_null ( context : ContextPtr ) -> WriteResult {
131
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_new_null ( context)
132
+ }
133
+ pub ( crate ) unsafe fn shopify_function_output_finalize ( context : ContextPtr ) -> WriteResult {
134
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_finalize ( context)
135
+ }
136
+ pub ( crate ) unsafe fn shopify_function_output_new_i32 (
137
+ context : ContextPtr ,
138
+ int : i32 ,
139
+ ) -> WriteResult {
140
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_new_i32 ( context, int)
141
+ }
142
+ pub ( crate ) unsafe fn shopify_function_output_new_f64 (
143
+ context : ContextPtr ,
144
+ float : f64 ,
145
+ ) -> WriteResult {
146
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_new_f64 ( context, float)
147
+ }
148
+ pub ( crate ) unsafe fn shopify_function_output_new_utf8_str (
149
+ context : ContextPtr ,
150
+ ptr : * const u8 ,
151
+ len : usize ,
152
+ ) -> WriteResult {
153
+ let result =
154
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_new_utf8_str (
155
+ context, len,
156
+ ) ;
157
+ let write_result =
158
+ WriteResult :: from_repr ( ( result >> 32 ) as u32 ) . expect ( "Invalid write result" ) ;
159
+ let dst = result as u32 ;
160
+ if write_result == WriteResult :: Ok {
161
+ std:: ptr:: copy ( ptr as _ , dst as _ , len) ;
162
+ }
163
+ write_result
164
+ }
165
+ pub ( crate ) unsafe fn shopify_function_output_new_interned_utf8_str (
166
+ context : ContextPtr ,
167
+ id : shopify_function_wasm_api_core:: InternedStringId ,
168
+ ) -> WriteResult {
169
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_new_interned_utf8_str (
170
+ context, id,
171
+ )
172
+ }
173
+ pub ( crate ) unsafe fn shopify_function_output_new_object (
174
+ context : ContextPtr ,
175
+ len : usize ,
176
+ ) -> WriteResult {
177
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_new_object ( context, len)
178
+ }
179
+ pub ( crate ) unsafe fn shopify_function_output_finish_object ( context : ContextPtr ) -> WriteResult {
180
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_finish_object ( context)
181
+ }
182
+ pub ( crate ) unsafe fn shopify_function_output_new_array (
183
+ context : ContextPtr ,
184
+ len : usize ,
185
+ ) -> WriteResult {
186
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_new_array ( context, len)
187
+ }
188
+ pub ( crate ) unsafe fn shopify_function_output_finish_array ( context : ContextPtr ) -> WriteResult {
189
+ shopify_function_wasm_api_provider:: write:: shopify_function_output_finish_array ( context)
190
+ }
191
+
192
+ // Other.
193
+ pub ( crate ) unsafe fn shopify_function_intern_utf8_str (
194
+ context : ContextPtr ,
195
+ ptr : * const u8 ,
196
+ len : usize ,
197
+ ) -> usize {
198
+ let result =
199
+ shopify_function_wasm_api_provider:: shopify_function_intern_utf8_str ( context, len) ;
200
+ let id = ( result >> usize:: BITS ) as usize ;
201
+ let dst = result as usize ;
202
+ std:: ptr:: copy ( ptr as _ , dst as _ , len) ;
203
+ id
204
+ }
205
+ }
206
+ #[ cfg( not( target_family = "wasm" ) ) ]
207
+ use provider_fallback:: * ;
208
+
65
209
#[ derive( Clone , Copy ) ]
66
210
pub struct InternedStringId ( shopify_function_wasm_api_core:: InternedStringId ) ;
67
211
@@ -110,12 +254,12 @@ impl Value {
110
254
pub fn as_string ( & self ) -> Option < String > {
111
255
match self . nan_box . try_decode ( ) {
112
256
Ok ( ValueRef :: String { ptr, len } ) => {
113
- let len = if len as u64 == NanBox :: MAX_VALUE_LENGTH {
257
+ let len = if len == NanBox :: MAX_VALUE_LENGTH {
114
258
unsafe {
115
259
shopify_function_input_get_val_len (
116
260
self . context . as_ptr ( ) as _ ,
117
261
self . nan_box . to_bits ( ) ,
118
- ) as usize
262
+ )
119
263
}
120
264
} else {
121
265
len
@@ -191,12 +335,12 @@ impl Value {
191
335
pub fn array_len ( & self ) -> Option < usize > {
192
336
match self . nan_box . try_decode ( ) {
193
337
Ok ( ValueRef :: Array { len, .. } ) => {
194
- let len = if len as u64 == NanBox :: MAX_VALUE_LENGTH {
338
+ let len = if len == NanBox :: MAX_VALUE_LENGTH {
195
339
unsafe {
196
340
shopify_function_input_get_val_len (
197
341
self . context . as_ptr ( ) as _ ,
198
342
self . nan_box . to_bits ( ) ,
199
- ) as usize
343
+ )
200
344
}
201
345
} else {
202
346
len
@@ -255,10 +399,26 @@ impl std::fmt::Display for ContextError {
255
399
}
256
400
257
401
impl Context {
402
+ #[ cfg( target_family = "wasm" ) ]
258
403
pub fn new ( ) -> Self {
259
404
Self ( unsafe { shopify_function_context_new ( ) } )
260
405
}
261
406
407
+ #[ cfg( not( target_family = "wasm" ) ) ]
408
+ pub fn new ( ) -> Self {
409
+ panic ! ( "Cannot run in non-WASM environment; use `new_with_input` instead" )
410
+ }
411
+
412
+ #[ cfg( not( target_family = "wasm" ) ) ]
413
+ pub fn new_with_input ( input : serde_json:: Value ) -> Self {
414
+ let bytes = rmp_serde:: to_vec ( & input) . unwrap ( ) ;
415
+ Self (
416
+ shopify_function_wasm_api_provider:: shopify_function_context_new_from_msgpack_bytes (
417
+ bytes,
418
+ ) ,
419
+ )
420
+ }
421
+
262
422
pub fn input_get ( & self ) -> Result < Value , ContextError > {
263
423
let val = unsafe { shopify_function_input_get ( self . 0 ) } ;
264
424
NonNull :: new ( self . 0 as _ )
0 commit comments