11// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22// SPDX-License-Identifier: Apache-2.0
3- mod quota_exceeded_error;
4-
53use core:: fmt;
64use std:: fmt:: Debug ;
75
86use llrt_utils:: {
7+ object:: define_subclass,
98 option:: Undefined ,
109 primordials:: { BasePrimordials , Primordial } ,
1110} ;
@@ -21,7 +20,6 @@ use rquickjs::{
2120 qjs, Class , Coerced , Ctx , Error , Exception , FromJs , IntoJs , JsLifetime , Object , Result , Value ,
2221} ;
2322
24- pub use crate :: quota_exceeded_error:: QuotaExceededError ;
2523use crate :: DOMExceptionName :: { NotSupportedError , OperationError , TypeMismatchError } ;
2624
2725#[ derive( Trace , JsLifetime , Debug ) ]
@@ -228,6 +226,46 @@ impl<'js> DOMException {
228226 pub fn operation_error ( ctx : & Ctx < ' js > , message : impl Into < String > ) -> Error {
229227 Self :: create_error ( ctx, OperationError , message)
230228 }
229+
230+ pub fn quota_exceeded_error ( ctx : & Ctx < ' js > , message : impl Into < String > ) -> Error {
231+ let value = Self :: create_quota_exceeded ( ctx, message. into ( ) )
232+ . expect ( "failed to create QuotaExceededError" ) ;
233+ Self :: throw_value ( ctx, value)
234+ }
235+
236+ fn create_quota_exceeded ( ctx : & Ctx < ' js > , message : String ) -> Result < Value < ' js > > {
237+ let ctor: Constructor = ctx. globals ( ) . get ( "QuotaExceededError" ) ?;
238+ ctor. construct ( ( message, ) )
239+ }
240+
241+ fn define_quota_exceeded_error ( ctx : & Ctx < ' js > ) -> Result < ( ) > {
242+ let dom_exception: Constructor = ctx. globals ( ) . get ( DOMException :: NAME ) ?;
243+ let quota_exceeded_error = define_subclass (
244+ ctx,
245+ "QuotaExceededError" ,
246+ & dom_exception,
247+ |ctx, message : Opt < Undefined < Coerced < String > > > | {
248+ let message = match message. 0 {
249+ Some ( Undefined ( Some ( m) ) ) => m. 0 ,
250+ _ => String :: new ( ) ,
251+ } ;
252+ DOMException :: new_with_name ( & ctx, DOMExceptionName :: QuotaExceededError , message)
253+ } ,
254+ ) ?;
255+ let null = Value :: new_null ( ctx. clone ( ) ) ;
256+ let proto: Object = quota_exceeded_error. get ( PredefinedAtom :: Prototype ) ?;
257+ proto. prop (
258+ "requested" ,
259+ Property :: from ( null. clone ( ) ) . enumerable ( ) . configurable ( ) ,
260+ ) ?;
261+ proto. prop ( "quota" , Property :: from ( null) . enumerable ( ) . configurable ( ) ) ?;
262+ ctx. globals ( ) . prop (
263+ "QuotaExceededError" ,
264+ Property :: from ( quota_exceeded_error)
265+ . writable ( )
266+ . configurable ( ) ,
267+ )
268+ }
231269}
232270
233271macro_rules! create_dom_exception {
@@ -349,22 +387,14 @@ pub fn init(ctx: &Ctx<'_>) -> Result<()> {
349387 let primordials = BasePrimordials :: get ( ctx) ?;
350388 dom_ex_proto. set_prototype ( Some ( & primordials. prototype_error ) ) ?;
351389
352- if let Some ( constructor) = Class :: < QuotaExceededError > :: create_constructor ( ctx) ? {
353- // the wpt tests expect this particular property descriptor
354- globals. prop (
355- QuotaExceededError :: NAME ,
356- Property :: from ( constructor) . writable ( ) . configurable ( ) ,
357- ) ?;
358- }
359-
360- let qee_ex_proto = Class :: < QuotaExceededError > :: prototype ( ctx) ?. unwrap ( ) ;
361- qee_ex_proto. set_prototype ( Some ( & primordials. prototype_error ) ) ?;
390+ DOMException :: define_quota_exceeded_error ( ctx) ?;
362391
363392 // `Error.isError(v)` only returns `true` for objects with QuickJS's
364393 // `[[ErrorData]]` internal slot (class id `JS_CLASS_ERROR`). There is
365394 // no public rquickjs API to tag a class-derived instance with that
366395 // slot, so we replace `Error.isError` with a version that also
367- // recognizes `DOMException` instances via `instanceof`.
396+ // recognizes `DOMException` instances (and its subclasses) via
397+ // `instanceof`.
368398 primordials
369399 . constructor_error
370400 . set ( "isError" , Func :: from ( is_error) ) ?;
@@ -380,9 +410,5 @@ fn is_error<'js>(ctx: Ctx<'js>, value: Value<'js>) -> Result<bool> {
380410 return Ok ( false ) ;
381411 } ;
382412 let dom_exception: Value = ctx. globals ( ) . get ( DOMException :: NAME ) ?;
383- if obj. is_instance_of ( & dom_exception) {
384- return Ok ( true ) ;
385- }
386- let quota_exceeded_error: Value = ctx. globals ( ) . get ( QuotaExceededError :: NAME ) ?;
387- Ok ( obj. is_instance_of ( & quota_exceeded_error) )
413+ Ok ( obj. is_instance_of ( & dom_exception) )
388414}
0 commit comments