@@ -858,6 +858,53 @@ static void slice_string_tocstring(void)
858858 JS_FreeRuntime (rt );
859859}
860860
861+ static void immutable_array_buffer (void )
862+ {
863+ JSValue obj , ret ;
864+ bool immutable ;
865+ char buf [96 ];
866+ int i , v ;
867+
868+ JSRuntime * rt = JS_NewRuntime ();
869+ JSContext * ctx = JS_NewContext (rt );
870+ for (i = 0 ; i < 2 ; i ++ ) {
871+ obj = JS_NewObject (ctx );
872+ immutable = (i == 0 );
873+ assert (-1 == JS_IsImmutableArrayBuffer (JS_NULL ));
874+ assert (-1 == JS_IsImmutableArrayBuffer (JS_UNDEFINED ));
875+ assert (-1 == JS_IsImmutableArrayBuffer (obj ));
876+ assert (-1 == JS_SetImmutableArrayBuffer (JS_NULL , immutable ));
877+ assert (-1 == JS_SetImmutableArrayBuffer (JS_UNDEFINED , immutable ));
878+ assert (-1 == JS_SetImmutableArrayBuffer (obj , immutable ));
879+ JS_FreeValue (ctx , obj );
880+ }
881+ obj = eval (ctx , "globalThis.ab = new ArrayBuffer(1)" );
882+ assert (!JS_IsException (obj ));
883+ assert (JS_IsArrayBuffer (obj ));
884+ assert (!JS_IsImmutableArrayBuffer (obj ));
885+ for (i = 1 ; i <= 3 ; i ++ ) {
886+ immutable = (i == 2 );
887+ if (i > 1 )
888+ JS_SetImmutableArrayBuffer (obj , immutable );
889+ assert (immutable == JS_IsImmutableArrayBuffer (obj ));
890+ snprintf (buf , sizeof (buf ),
891+ "var ta = new Uint8Array(ab); ta[0] = %d; ta[0]" , i );
892+ ret = eval (ctx , buf );
893+ assert (!JS_IsException (ret ));
894+ assert (JS_IsNumber (ret ));
895+ assert (0 == JS_ToInt32 (ctx , & v , ret ));
896+ JS_FreeValue (ctx , ret );
897+ if (immutable ) {
898+ assert (v != i );
899+ } else {
900+ assert (v == i );
901+ }
902+ }
903+ JS_FreeValue (ctx , obj );
904+ JS_FreeContext (ctx );
905+ JS_FreeRuntime (rt );
906+ }
907+
861908int main (void )
862909{
863910 cfunctions ();
@@ -875,5 +922,6 @@ int main(void)
875922 new_errors ();
876923 global_object_prototype ();
877924 slice_string_tocstring ();
925+ immutable_array_buffer ();
878926 return 0 ;
879927}
0 commit comments