@@ -1035,7 +1035,8 @@ export class QuickJSContext
1035
1035
// Evaluation ---------------------------------------------------------------
1036
1036
1037
1037
/**
1038
- * [`func.call(thisVal, ...args)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call).
1038
+ * [`func.call(thisVal, ...args)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) or
1039
+ * [`func.apply(thisVal, args)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply).
1039
1040
* Call a JSValue as a function.
1040
1041
*
1041
1042
* See {@link unwrapResult}, which will throw if the function returned an error, or
@@ -1046,13 +1047,40 @@ export class QuickJSContext
1046
1047
* @returns A result. If the function threw synchronously, `result.error` be a
1047
1048
* handle to the exception. Otherwise `result.value` will be a handle to the
1048
1049
* value.
1050
+ *
1051
+ * Example:
1052
+ *
1053
+ * ```typescript
1054
+ * using parseIntHandle = context.getProp(global, "parseInt")
1055
+ * using stringHandle = context.newString("42")
1056
+ * using resultHandle = context.callFunction(parseIntHandle, context.undefined, stringHandle).unwrap()
1057
+ * console.log(context.dump(resultHandle)) // 42
1058
+ * ```
1049
1059
*/
1060
+ callFunction (
1061
+ func : QuickJSHandle ,
1062
+ thisVal : QuickJSHandle ,
1063
+ args ?: QuickJSHandle [ ] ,
1064
+ ) : ContextResult < QuickJSHandle >
1050
1065
callFunction (
1051
1066
func : QuickJSHandle ,
1052
1067
thisVal : QuickJSHandle ,
1053
1068
...args : QuickJSHandle [ ]
1069
+ ) : ContextResult < QuickJSHandle >
1070
+ callFunction (
1071
+ func : QuickJSHandle ,
1072
+ thisVal : QuickJSHandle ,
1073
+ ...restArgs : Array < QuickJSHandle | QuickJSHandle [ ] | undefined >
1054
1074
) : ContextResult < QuickJSHandle > {
1055
1075
this . runtime . assertOwned ( func )
1076
+ let args
1077
+ const firstArg = restArgs [ 0 ]
1078
+ if ( firstArg === undefined || Array . isArray ( firstArg ) ) {
1079
+ args = firstArg ?? [ ]
1080
+ } else {
1081
+ args = restArgs as QuickJSHandle [ ]
1082
+ }
1083
+
1056
1084
const resultPtr = this . memory
1057
1085
. toPointerArray ( args )
1058
1086
. consume ( ( argsArrayPtr ) =>
@@ -1089,7 +1117,7 @@ export class QuickJSContext
1089
1117
args : QuickJSHandle [ ] = [ ] ,
1090
1118
) : ContextResult < QuickJSHandle > {
1091
1119
return this . getProp ( thisHandle , key ) . consume ( ( func ) =>
1092
- this . callFunction ( func , thisHandle , ... args ) ,
1120
+ this . callFunction ( func , thisHandle , args ) ,
1093
1121
)
1094
1122
}
1095
1123
0 commit comments