@@ -855,7 +855,18 @@ export class QuickJSContext
855
855
}
856
856
857
857
/**
858
- * `handle.length`.
858
+ * `handle.length` as a host number.
859
+ *
860
+ * Example use:
861
+ * ```typescript
862
+ * const length = context.getLength(arrayHandle) ?? 0
863
+ * for (let i = 0; i < length; i++) {
864
+ * using value = context.getProp(arrayHandle, i)
865
+ * console.log(`array[${i}] =`, context.dump(value))
866
+ * }
867
+ * ```
868
+ *
869
+ * @returns a number if the handle has a numeric length property, otherwise `undefined`.
859
870
*/
860
871
getLength ( handle : QuickJSHandle ) : number | undefined {
861
872
this . runtime . assertOwned ( handle )
@@ -871,19 +882,36 @@ export class QuickJSContext
871
882
* Similar to the [standard semantics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames),
872
883
* but with extra, non-standard options for:
873
884
*
874
- * - fetching array indexes as numbers
875
- * - including symbols
876
- * - only iterating over enumerable properties
885
+ * - fetching array indexes as numbers (`numbers: true`)
886
+ * - including symbols (`symbols: true`)
887
+ * - only iterating over enumerable properties (`onlyEnumerable: true`)
877
888
*
878
- * To emulate the standard, use these options :
889
+ * The default behavior is to emulate the standard :
879
890
* ```typescript
880
891
* context.getOwnPropertyNames(handle, { strings: true, numbersAsStrings: true })
881
892
* ```
893
+ *
894
+ * Note when passing an explicit options object, you must set at least one
895
+ * option, and `strings` are not included unless specified.
896
+ *
897
+ * Example use:
898
+ * ```typescript
899
+ * for (using prop of context.getOwnPropertyNames(objectHandle).unwrap()) {
900
+ * using value = context.getProp(handle, prop)
901
+ * console.log(context.dump(prop), '->', context.dump(value))
902
+ * }
903
+ * ```
904
+ *
905
+ * @returns an an array of handles of the property names. The array itself is disposable for your convenience.
906
+ * @throws QuickJSEmptyGetOwnPropertyNames if no options are set.
882
907
*/
883
908
getOwnPropertyNames (
884
909
handle : QuickJSHandle ,
885
- options : GetOwnPropertyNamesOptions ,
886
- ) : ContextResult < DisposableArray < JSValue > > {
910
+ options : GetOwnPropertyNamesOptions = {
911
+ strings : true ,
912
+ numbersAsStrings : true ,
913
+ } ,
914
+ ) : ContextResult < DisposableArray < QuickJSHandle > > {
887
915
this . runtime . assertOwned ( handle )
888
916
handle . value // assert alive
889
917
const flags = getOwnPropertyNamesOptionsToFlags ( options )
@@ -923,14 +951,10 @@ export class QuickJSContext
923
951
* is considered done if the handle is disposed.
924
952
*
925
953
* ```typescript
926
- * for (const nextResult of context.unwrapResult(context.getIterator(arrayHandle)) {
927
- * const nextHandle = context.unwrapResult(nextResult)
928
- * try {
929
- * // Do something with nextHandle
930
- * console.log(context.dump(nextHandle))
931
- * } finally {
932
- * nextHandle.dispose()
933
- * }
954
+ * for (using entriesHandle of context.getIterator(mapHandle).unwrap()) {
955
+ * using keyHandle = context.getProp(entriesHandle, 0)
956
+ * using valueHandle = context.getProp(entriesHandle, 1)
957
+ * console.log(context.dump(keyHandle), '->', context.dump(valueHandle))
934
958
* }
935
959
* ```
936
960
*/
0 commit comments