@@ -226,6 +226,7 @@ pub enum FFITypeId {
226226 Double = 3 ,
227227 Bool = 4 ,
228228 Bytes = 5 ,
229+ JsonPayload = 6 ,
229230}
230231
231232impl FFITypeId {
@@ -238,6 +239,7 @@ impl FFITypeId {
238239 3 => Some ( FFITypeId :: Double ) ,
239240 4 => Some ( FFITypeId :: Bool ) ,
240241 5 => Some ( FFITypeId :: Bytes ) ,
242+ 6 => Some ( FFITypeId :: JsonPayload ) ,
241243 _ => None ,
242244 }
243245 }
@@ -287,15 +289,10 @@ pub fn c_args_to_v8_args_global(
287289 v8:: Boolean :: new ( scope, bool_value) . into ( )
288290 }
289291 Some ( FFITypeId :: Bytes ) => {
290- let byte_array =
291- unsafe { std:: slice:: from_raw_parts ( arg_ptr as * const u8 , size as usize ) } ;
292-
293- let v8_array = v8:: ArrayBuffer :: new_backing_store_from_boxed_slice (
294- byte_array. to_vec ( ) . into_boxed_slice ( ) ,
295- ) ;
296- let v8_shared_array = v8_array. make_shared ( ) ;
297- let v8_buffer = v8:: ArrayBuffer :: with_backing_store ( scope, & v8_shared_array) ;
298- v8_buffer. into ( )
292+ parse_byte_data ( scope, arg_ptr as * const u8 , size as usize ) . into ( )
293+ }
294+ Some ( FFITypeId :: JsonPayload ) => {
295+ parse_json_payload_bytes ( scope, arg_ptr as * const u8 , size as usize ) . into ( )
299296 }
300297 _ => v8:: undefined ( scope) . into ( ) ,
301298 } ;
@@ -307,6 +304,50 @@ pub fn c_args_to_v8_args_global(
307304 v8_args
308305}
309306
307+ fn parse_byte_data < ' a > (
308+ scope : & mut v8:: HandleScope < ' a > ,
309+ arg_ptr : * const u8 ,
310+ size : usize ,
311+ ) -> v8:: Local < ' a , v8:: ArrayBuffer > {
312+ let byte_array = unsafe { std:: slice:: from_raw_parts ( arg_ptr, size) } ;
313+
314+ let v8_array =
315+ v8:: ArrayBuffer :: new_backing_store_from_boxed_slice ( byte_array. to_vec ( ) . into_boxed_slice ( ) ) ;
316+ let v8_shared_array = v8_array. make_shared ( ) ;
317+ let v8_buffer = v8:: ArrayBuffer :: with_backing_store ( scope, & v8_shared_array) ;
318+ v8_buffer
319+ }
320+
321+ fn parse_json_payload_bytes < ' a > (
322+ scope : & mut v8:: HandleScope < ' a > ,
323+ arg_ptr : * const u8 ,
324+ size : usize ,
325+ ) -> v8:: Local < ' a , v8:: Value > {
326+ let v8_buffer = parse_byte_data ( scope, arg_ptr, size) ;
327+
328+ // call JsonPayload.decode() from the runtime add pass the v8_buffer as an argument
329+ let json_payload = v8:: String :: new ( scope, "JsonPayload" ) . unwrap ( ) ;
330+ let json_payload_value = scope
331+ . get_current_context ( )
332+ . global ( scope)
333+ . get ( scope, json_payload. into ( ) )
334+ . unwrap ( ) ;
335+
336+ let decode_function = v8:: String :: new ( scope, "decode" ) . unwrap ( ) ;
337+ let decode_function_value = json_payload_value
338+ . to_object ( scope)
339+ . unwrap ( )
340+ . get ( scope, decode_function. into ( ) )
341+ . unwrap ( ) ;
342+ let decode_function = v8:: Local :: < v8:: Function > :: try_from ( decode_function_value) . unwrap ( ) ;
343+ let args = vec ! [ v8_buffer. into( ) ] ;
344+ let result = decode_function
345+ . call ( scope, json_payload_value. into ( ) , & args)
346+ . unwrap ( ) ;
347+ let result = v8:: Local :: < v8:: Value > :: try_from ( result) . unwrap ( ) ;
348+ result
349+ }
350+
310351pub fn c_args_to_v8_args_local < ' s > (
311352 scope : & mut v8:: HandleScope < ' s > ,
312353 args : * const * const c_void ,
@@ -350,15 +391,10 @@ pub fn c_args_to_v8_args_local<'s>(
350391 v8:: Boolean :: new ( scope, bool_value) . into ( )
351392 }
352393 Some ( FFITypeId :: Bytes ) => {
353- let byte_array =
354- unsafe { std:: slice:: from_raw_parts ( arg_ptr as * const u8 , size as usize ) } ;
355-
356- let v8_array = v8:: ArrayBuffer :: new_backing_store_from_boxed_slice (
357- byte_array. to_vec ( ) . into_boxed_slice ( ) ,
358- ) ;
359- let v8_shared_array = v8_array. make_shared ( ) ;
360- let v8_buffer = v8:: ArrayBuffer :: with_backing_store ( scope, & v8_shared_array) ;
361- v8_buffer. into ( )
394+ parse_byte_data ( scope, arg_ptr as * const u8 , size as usize ) . into ( )
395+ }
396+ Some ( FFITypeId :: JsonPayload ) => {
397+ parse_json_payload_bytes ( scope, arg_ptr as * const u8 , size as usize ) . into ( )
362398 }
363399 _ => v8:: undefined ( scope) . into ( ) ,
364400 } ;
0 commit comments