@@ -116,3 +116,57 @@ impl<'js, Ok: FromJs<'js>, Err: FromJs<'js>> FromJs<'js> for JsResult<Ok, Err> {
116116 }
117117 }
118118}
119+
120+ // Wrapper type that forces the js type to be a bigint instead of the default number which can loose some bits due to
121+ pub struct BigIntWrapper < T > ( pub T ) ;
122+
123+ impl < ' js > IntoJs < ' js > for BigIntWrapper < u64 > {
124+ fn into_js ( self , ctx : & Ctx < ' js > ) -> rquickjs:: Result < Value < ' js > > {
125+ let bigint = rquickjs:: BigInt :: from_u64 ( ctx. clone ( ) , self . 0 ) ?;
126+ Ok ( Value :: from_big_int ( bigint) )
127+ }
128+ }
129+
130+ impl < ' js > IntoJs < ' js > for BigIntWrapper < i64 > {
131+ fn into_js ( self , ctx : & Ctx < ' js > ) -> rquickjs:: Result < Value < ' js > > {
132+ let bigint = rquickjs:: BigInt :: from_i64 ( ctx. clone ( ) , self . 0 ) ?;
133+ Ok ( Value :: from_big_int ( bigint) )
134+ }
135+ }
136+
137+ impl < ' js > FromJs < ' js > for BigIntWrapper < u64 > {
138+ fn from_js ( ctx : & Ctx < ' js > , value : Value < ' js > ) -> rquickjs:: Result < Self > {
139+ let bigint = rquickjs:: BigInt :: from_js ( ctx, value) ?;
140+ let i64_value = bigint. to_i64 ( ) ?;
141+ Ok ( BigIntWrapper ( i64_value as u64 ) )
142+ }
143+ }
144+
145+ impl < ' js > FromJs < ' js > for BigIntWrapper < i64 > {
146+ fn from_js ( ctx : & Ctx < ' js > , value : Value < ' js > ) -> rquickjs:: Result < Self > {
147+ let bigint = rquickjs:: BigInt :: from_js ( ctx, value) ?;
148+ let i64_value = bigint. to_i64 ( ) ?;
149+ Ok ( BigIntWrapper ( i64_value) )
150+ }
151+ }
152+
153+ pub struct UInt8Array ( pub Vec < u8 > ) ;
154+
155+ impl < ' js > IntoJs < ' js > for UInt8Array {
156+ fn into_js ( self , ctx : & Ctx < ' js > ) -> rquickjs:: Result < Value < ' js > > {
157+ let array = rquickjs:: TypedArray :: new_copy ( ctx. clone ( ) , self . 0 ) ?;
158+ Ok ( array. into_value ( ) )
159+ }
160+ }
161+
162+ impl < ' js > FromJs < ' js > for UInt8Array {
163+ fn from_js ( _ctx : & Ctx < ' js > , value : Value < ' js > ) -> rquickjs:: Result < Self > {
164+ let array = rquickjs:: TypedArray :: < ' js , u8 > :: from_value ( value) ?;
165+ Ok ( UInt8Array (
166+ array
167+ . as_bytes ( )
168+ . expect ( "the UInt8Array passed to decode is detached" )
169+ . to_vec ( ) ,
170+ ) )
171+ }
172+ }
0 commit comments