Skip to content

Commit 28f8688

Browse files
committed
improve iteration docs and add defaults to getOwnPropertyNames
1 parent 2b9369d commit 28f8688

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

packages/quickjs-emscripten-core/src/context.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,18 @@ export class QuickJSContext
855855
}
856856

857857
/**
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`.
859870
*/
860871
getLength(handle: QuickJSHandle): number | undefined {
861872
this.runtime.assertOwned(handle)
@@ -871,19 +882,36 @@ export class QuickJSContext
871882
* Similar to the [standard semantics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames),
872883
* but with extra, non-standard options for:
873884
*
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`)
877888
*
878-
* To emulate the standard, use these options:
889+
* The default behavior is to emulate the standard:
879890
* ```typescript
880891
* context.getOwnPropertyNames(handle, { strings: true, numbersAsStrings: true })
881892
* ```
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.
882907
*/
883908
getOwnPropertyNames(
884909
handle: QuickJSHandle,
885-
options: GetOwnPropertyNamesOptions,
886-
): ContextResult<DisposableArray<JSValue>> {
910+
options: GetOwnPropertyNamesOptions = {
911+
strings: true,
912+
numbersAsStrings: true,
913+
},
914+
): ContextResult<DisposableArray<QuickJSHandle>> {
887915
this.runtime.assertOwned(handle)
888916
handle.value // assert alive
889917
const flags = getOwnPropertyNamesOptionsToFlags(options)
@@ -923,14 +951,10 @@ export class QuickJSContext
923951
* is considered done if the handle is disposed.
924952
*
925953
* ```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))
934958
* }
935959
* ```
936960
*/

0 commit comments

Comments
 (0)