@@ -11,37 +11,42 @@ import SubstrataQuickJS
1111// MARK: - Call conversion funcs
1212
1313internal func returnJSValueRef( context: JSContext , function: JSFunctionDefinition , args: [ JSConvertible ] ) -> JSValue {
14- // make sure we're consistent with our types.
15- // nil = undefined in js.
16- // nsnull = null in js.
17- var result = JSValue . undefined
18- let v = function ( args) as? JSInternalConvertible
19- if let v = v? . toJSValue ( context: context) {
20- result = v
14+ do {
15+ let v = try function ( args) as? JSInternalConvertible
16+ if let v = v? . toJSValue ( context: context) {
17+ return v
18+ }
19+ return JSValue . undefined
20+ } catch {
21+ return context. throwError ( error)
2122 }
22-
23- return result
2423}
2524
2625internal func returnJSValueRef( context: JSContext , function: JSPropertyGetterDefinition ) -> JSValue {
2726 // make sure we're consistent with our types.
2827 // nil = undefined in js.
2928 // nsnull = null in js.
30- var result = JSValue . undefined
31- let v = function ( ) as? JSInternalConvertible
32- if let v = v? . toJSValue ( context: context) {
33- result = v
29+ do {
30+ let v = try function ( ) as? JSInternalConvertible
31+ if let v = v? . toJSValue ( context: context) {
32+ return v
33+ }
34+ return JSValue . undefined
35+ } catch {
36+ return context. throwError ( error)
3437 }
35-
36- return result
3738}
3839
3940internal func returnJSValueRef( context: JSContext , function: JSPropertySetterDefinition , arg: JSConvertible ? ) -> JSValue {
4041 // make sure we're consistent with our types.
4142 // nil = undefined in js.
4243 // nsnull = null in js.
43- function ( arg)
44- return JSValue . undefined
44+ do {
45+ try function ( arg)
46+ return JSValue . undefined
47+ } catch {
48+ return context. throwError ( error)
49+ }
4550}
4651
4752internal func jsArgsToTypes( context: JSContext ? , argc: Int32 , argv: UnsafeMutablePointer < JSValue > ? ) -> [ JSConvertible ] {
@@ -610,11 +615,45 @@ public final class JSError: JSConvertible, JSInternalConvertible {
610615 }
611616
612617 internal func toJSValue( context: JSContext ) -> JSValue ? {
613- // it's not expected that JS errors are created in native
614- // and flow back into JS.
615- return nil
618+ // Create the base error object
619+ let errorValue = JS_NewError ( context. ref)
620+
621+ // Set the message if we have it
622+ if let message = self . message {
623+ let messageAtom = JS_NewAtom ( context. ref, " message " )
624+ let messageValue = JS_NewString ( context. ref, message)
625+ JS_SetProperty ( context. ref, errorValue, messageAtom, messageValue)
626+ JS_FreeAtom ( context. ref, messageAtom)
627+ }
628+
629+ // Set the name if we have it
630+ if let name = self . name {
631+ let nameAtom = JS_NewAtom ( context. ref, " name " )
632+ let nameValue = JS_NewString ( context. ref, name)
633+ JS_SetProperty ( context. ref, errorValue, nameAtom, nameValue)
634+ JS_FreeAtom ( context. ref, nameAtom)
635+ }
636+
637+ // Set the cause if we have it
638+ if let cause = self . cause {
639+ let causeAtom = JS_NewAtom ( context. ref, " cause " )
640+ let causeValue = JS_NewString ( context. ref, cause)
641+ JS_SetProperty ( context. ref, errorValue, causeAtom, causeValue)
642+ JS_FreeAtom ( context. ref, causeAtom)
643+ }
644+
645+ // Set the stack if we have it
646+ if let stack = self . stack {
647+ let stackAtom = JS_NewAtom ( context. ref, " stack " )
648+ let stackValue = JS_NewString ( context. ref, stack)
649+ JS_SetProperty ( context. ref, errorValue, stackAtom, stackValue)
650+ JS_FreeAtom ( context. ref, stackAtom)
651+ }
652+
653+ return errorValue
616654 }
617655
656+
618657 public var string : String {
619658 return """
620659 Javascript Error:
@@ -624,6 +663,10 @@ public final class JSError: JSConvertible, JSInternalConvertible {
624663 """
625664 }
626665
666+ static func from( _ error: Error ) -> JSError {
667+ return JSError ( message: error. localizedDescription)
668+ }
669+
627670 internal init ( value: JSValue , context: JSContext ) {
628671 name = Self . value ( for: " name " , object: value, context: context)
629672 message = Self . value ( for: " message " , object: value, context: context)
@@ -632,6 +675,13 @@ public final class JSError: JSConvertible, JSInternalConvertible {
632675 stack = Self . value ( for: " stack " , object: value, context: context)
633676 }
634677
678+ internal init ( name: String ? = " Native Code Exception " , message: String ? , cause: String ? = nil , stack: String ? = nil ) {
679+ self . name = name
680+ self . message = message
681+ self . cause = cause
682+ self . stack = stack
683+ }
684+
635685 internal let name : String ?
636686 internal let message : String ?
637687 internal let cause : String ?
0 commit comments