@@ -18,8 +18,9 @@ import { QuickJSDeferredPromise } from "./deferred-promise"
18
18
// eslint-disable-next-line @typescript-eslint/no-unused-vars
19
19
import type { shouldInterruptAfterDeadline } from "./interrupt-helpers"
20
20
import { QuickJSPromisePending , QuickJSUnwrapError } from "./errors"
21
- import type { Disposable , DisposableArray } from "./lifetime"
21
+ import type { Disposable , DisposableArray , DisposableFail , DisposableSuccess } from "./lifetime"
22
22
import {
23
+ DisposableResult ,
23
24
Lifetime ,
24
25
Scope ,
25
26
StaticLifetime ,
@@ -35,25 +36,25 @@ import type {
35
36
// eslint-disable-next-line @typescript-eslint/no-unused-vars
36
37
ExecutePendingJobsResult ,
37
38
} from "./runtime"
38
- import {
39
+ import type {
39
40
ContextEvalOptions ,
40
- IsEqualOp ,
41
41
GetOwnPropertyNamesOptions ,
42
42
JSValue ,
43
43
PromiseExecutor ,
44
44
QuickJSHandle ,
45
45
StaticJSValue ,
46
46
} from "./types"
47
- import { evalOptionsToFlags , getOwnPropertyNamesOptionsToFlags } from "./types"
47
+ import { IsEqualOp , evalOptionsToFlags , getOwnPropertyNamesOptionsToFlags } from "./types"
48
48
import type {
49
49
LowLevelJavascriptVm ,
50
50
SuccessOrFail ,
51
- VmCallResult ,
52
51
VmFunctionImplementation ,
53
52
VmPropertyDescriptor ,
54
53
} from "./vm-interface"
55
54
import { QuickJSIterator } from "./QuickJSIterator"
56
55
56
+ export type ContextResult < S > = DisposableResult < S , QuickJSHandle >
57
+
57
58
/**
58
59
* Property key for getting or setting a property on a handle with
59
60
* {@link QuickJSContext#getProp}, {@link QuickJSContext#setProp}, or {@link QuickJSContext#defineProp}.
@@ -744,7 +745,7 @@ export class QuickJSContext
744
745
*
745
746
* @param promiseLikeHandle - A handle to a Promise-like value with a `.then(onSuccess, onError)` method.
746
747
*/
747
- resolvePromise ( promiseLikeHandle : QuickJSHandle ) : Promise < VmCallResult < QuickJSHandle > > {
748
+ resolvePromise ( promiseLikeHandle : QuickJSHandle ) : Promise < ContextResult < QuickJSHandle > > {
748
749
this . runtime . assertOwned ( promiseLikeHandle )
749
750
const vmResolveResult = Scope . withScope ( ( scope ) => {
750
751
const vmPromise = scope . manage ( this . getProp ( this . global , "Promise" ) )
@@ -755,25 +756,25 @@ export class QuickJSContext
755
756
return Promise . resolve ( vmResolveResult )
756
757
}
757
758
758
- return new Promise < VmCallResult < QuickJSHandle > > ( ( resolve ) => {
759
+ return new Promise < ContextResult < QuickJSHandle > > ( ( resolve ) => {
759
760
Scope . withScope ( ( scope ) => {
760
761
const resolveHandle = scope . manage (
761
762
this . newFunction ( "resolve" , ( value ) => {
762
- resolve ( { value : value && value . dup ( ) } )
763
+ resolve ( this . success ( value && value . dup ( ) ) )
763
764
} ) ,
764
765
)
765
766
766
767
const rejectHandle = scope . manage (
767
768
this . newFunction ( "reject" , ( error ) => {
768
- resolve ( { error : error && error . dup ( ) } )
769
+ resolve ( this . fail ( error && error . dup ( ) ) )
769
770
} ) ,
770
771
)
771
772
772
773
const promiseHandle = scope . manage ( vmResolveResult . value )
773
774
const promiseThenHandle = scope . manage ( this . getProp ( promiseHandle , "then" ) )
774
- this . unwrapResult (
775
- this . callFunction ( promiseThenHandle , promiseHandle , resolveHandle , rejectHandle ) ,
776
- ) . dispose ( )
775
+ this . callFunction ( promiseThenHandle , promiseHandle , resolveHandle , rejectHandle )
776
+ . unwrap ( )
777
+ . dispose ( )
777
778
} )
778
779
} )
779
780
}
@@ -861,7 +862,7 @@ export class QuickJSContext
861
862
getPropNames (
862
863
handle : QuickJSHandle ,
863
864
options : GetOwnPropertyNamesOptions ,
864
- ) : SuccessOrFail < DisposableArray < JSValue > , QuickJSHandle > {
865
+ ) : ContextResult < DisposableArray < JSValue > > {
865
866
this . runtime . assertOwned ( handle )
866
867
handle . value // assert alive
867
868
const flags = getOwnPropertyNamesOptionsToFlags ( options )
@@ -875,7 +876,7 @@ export class QuickJSContext
875
876
flags ,
876
877
)
877
878
if ( errorPtr ) {
878
- return { error : this . memory . heapValueHandle ( errorPtr ) }
879
+ return this . fail ( this . memory . heapValueHandle ( errorPtr ) )
879
880
}
880
881
const len = this . uint32Out . value . typedArray [ 0 ]
881
882
const ptr = outPtr . value . typedArray [ 0 ]
@@ -884,7 +885,7 @@ export class QuickJSContext
884
885
this . memory . heapValueHandle ( ptr as JSValuePointer ) ,
885
886
)
886
887
this . module . _free ( ptr )
887
- return { value : createDisposableArray ( handles ) }
888
+ return this . success ( createDisposableArray ( handles ) )
888
889
} )
889
890
}
890
891
@@ -907,17 +908,17 @@ export class QuickJSContext
907
908
* }
908
909
* ```
909
910
*/
910
- getIterator ( handle : QuickJSHandle ) : SuccessOrFail < QuickJSIterator , QuickJSHandle > {
911
+ getIterator ( handle : QuickJSHandle ) : ContextResult < QuickJSIterator > {
911
912
const SymbolIterator = ( this . _SymbolIterator ??= this . memory . manage (
912
913
this . getWellKnownSymbol ( "iterator" ) ,
913
914
) )
914
915
return Scope . withScope ( ( scope ) => {
915
916
const methodHandle = scope . manage ( this . getProp ( handle , SymbolIterator ) )
916
917
const iteratorCallResult = this . callFunction ( methodHandle , handle )
917
918
if ( iteratorCallResult . error ) {
918
- return iteratorCallResult
919
+ return iteratorCallResult . cast ( )
919
920
}
920
- return { value : new QuickJSIterator ( iteratorCallResult . value , this ) }
921
+ return this . success ( new QuickJSIterator ( iteratorCallResult . value , this ) )
921
922
} )
922
923
}
923
924
@@ -1000,7 +1001,7 @@ export class QuickJSContext
1000
1001
func : QuickJSHandle ,
1001
1002
thisVal : QuickJSHandle ,
1002
1003
...args : QuickJSHandle [ ]
1003
- ) : VmCallResult < QuickJSHandle > {
1004
+ ) : ContextResult < QuickJSHandle > {
1004
1005
this . runtime . assertOwned ( func )
1005
1006
const resultPtr = this . memory
1006
1007
. toPointerArray ( args )
@@ -1017,10 +1018,10 @@ export class QuickJSContext
1017
1018
const errorPtr = this . ffi . QTS_ResolveException ( this . ctx . value , resultPtr )
1018
1019
if ( errorPtr ) {
1019
1020
this . ffi . QTS_FreeValuePointer ( this . ctx . value , resultPtr )
1020
- return { error : this . memory . heapValueHandle ( errorPtr ) }
1021
+ return this . fail ( this . memory . heapValueHandle ( errorPtr ) )
1021
1022
}
1022
1023
1023
- return { value : this . memory . heapValueHandle ( resultPtr ) }
1024
+ return this . success ( this . memory . heapValueHandle ( resultPtr ) )
1024
1025
}
1025
1026
1026
1027
/**
@@ -1066,7 +1067,7 @@ export class QuickJSContext
1066
1067
* See {@link EvalFlags} for number semantics.
1067
1068
*/
1068
1069
options ?: number | ContextEvalOptions ,
1069
- ) : VmCallResult < QuickJSHandle > {
1070
+ ) : ContextResult < QuickJSHandle > {
1070
1071
const detectModule = ( options === undefined ? 1 : 0 ) as EvalDetectModule
1071
1072
const flags = evalOptionsToFlags ( options ) as EvalFlags
1072
1073
const resultPtr = this . memory
@@ -1084,9 +1085,9 @@ export class QuickJSContext
1084
1085
const errorPtr = this . ffi . QTS_ResolveException ( this . ctx . value , resultPtr )
1085
1086
if ( errorPtr ) {
1086
1087
this . ffi . QTS_FreeValuePointer ( this . ctx . value , resultPtr )
1087
- return { error : this . memory . heapValueHandle ( errorPtr ) }
1088
+ return this . fail ( this . memory . heapValueHandle ( errorPtr ) )
1088
1089
}
1089
- return { value : this . memory . heapValueHandle ( resultPtr ) }
1090
+ return this . success ( this . memory . heapValueHandle ( resultPtr ) )
1090
1091
}
1091
1092
1092
1093
/**
@@ -1326,4 +1327,12 @@ export class QuickJSContext
1326
1327
const ptr = this . ffi . QTS_bjson_decode ( this . ctx . value , handle . value )
1327
1328
return this . memory . heapValueHandle ( ptr )
1328
1329
}
1330
+
1331
+ protected success < S > ( value : S ) : DisposableSuccess < S , QuickJSHandle > {
1332
+ return DisposableResult . success ( value )
1333
+ }
1334
+
1335
+ protected fail < S > ( error : QuickJSHandle ) : DisposableFail < S , QuickJSHandle > {
1336
+ return DisposableResult . fail ( error , ( error ) => this . unwrapResult ( error ) )
1337
+ }
1329
1338
}
0 commit comments