Skip to content

Commit 310f072

Browse files
committed
getPropNames -> getOwnPropertyNames
1 parent 382448e commit 310f072

File tree

4 files changed

+53
-22
lines changed

4 files changed

+53
-22
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import type { JSPromiseState } from "./deferred-promise"
1818
import { QuickJSDeferredPromise } from "./deferred-promise"
1919
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2020
import type { shouldInterruptAfterDeadline } from "./interrupt-helpers"
21-
import { QuickJSNotImplemented, QuickJSPromisePending, QuickJSUnwrapError } from "./errors"
21+
import {
22+
QuickJSEmptyGetOwnPropertyNames,
23+
QuickJSNotImplemented,
24+
QuickJSPromisePending,
25+
QuickJSUnwrapError,
26+
} from "./errors"
2227
import type { Disposable, DisposableArray, DisposableFail, DisposableSuccess } from "./lifetime"
2328
import {
2429
DisposableResult,
@@ -865,13 +870,16 @@ export class QuickJSContext
865870
* `Object.getOwnPropertyNames(handle)`.
866871
* Similar to the [standard semantics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames).
867872
*/
868-
getPropNames(
873+
getOwnPropertyNames(
869874
handle: QuickJSHandle,
870875
options: GetOwnPropertyNamesOptions,
871876
): ContextResult<DisposableArray<JSValue>> {
872877
this.runtime.assertOwned(handle)
873878
handle.value // assert alive
874879
const flags = getOwnPropertyNamesOptionsToFlags(options)
880+
if (flags === 0) {
881+
throw new QuickJSEmptyGetOwnPropertyNames("No options set, will return an empty array")
882+
}
875883
return Scope.withScope((scope) => {
876884
const outPtr = scope.manage(
877885
this.memory.newMutablePointerArray<JSValuePointerPointerPointer>(1),
@@ -887,7 +895,6 @@ export class QuickJSContext
887895
return this.fail(this.memory.heapValueHandle(errorPtr))
888896
}
889897
const len = this.uint32Out.value.typedArray[0]
890-
console.log("getPropNames length", len)
891898
const ptr = outPtr.value.typedArray[0]
892899
const pointerArray = new Uint32Array(this.module.HEAP8.buffer, ptr, len)
893900
const handles = Array.from(pointerArray).map((ptr) =>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ export class QuickJSUnknownIntrinsic extends TypeError {
4848
export class QuickJSPromisePending extends Error {
4949
name = "QuickJSPromisePending"
5050
}
51+
52+
export class QuickJSEmptyGetOwnPropertyNames extends Error {
53+
name = "QuickJSEmptyGetOwnPropertyNames"
54+
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type JSValueConst = Lifetime<JSValueConstPointer, JSValuePointer, QuickJS
4141
* You can do so from Javascript by calling the .dispose() method.
4242
*/
4343
export type JSValue = Lifetime<JSValuePointer, JSValuePointer, QuickJSRuntime>
44+
// | Lifetime<JSValueUInt8Array, JSValueUInt8Array, QuickJSRuntime>
4445

4546
/**
4647
* Wraps a C pointer to a QuickJS JSValue, which represents a Javascript value inside
@@ -285,15 +286,15 @@ export function evalOptionsToFlags(evalOptions: ContextEvalOptions | number | un
285286

286287
export interface GetOwnPropertyNamesOptions {
287288
/** Include number properties like array indexes *as numbers* in the result. This is not standards-compliant */
288-
includeNumbers?: boolean
289+
numbers?: boolean
289290
/** Enable standards-compliant number properties. When set, `includeNumbers` is ignored. */
290291
numbersAsStrings?: boolean
291292
/** Include strings in the result */
292-
includeStrings?: boolean
293+
strings?: boolean
293294
/** Include symbols in the result */
294-
includeSymbols?: boolean
295-
/** Include private properties in the result */
296-
includePrivate?: boolean
295+
symbols?: boolean
296+
/** Include implementation-specific private properties in the result */
297+
quickjsPrivate?: boolean
297298
/** Only include the enumerable properties */
298299
onlyEnumerable?: boolean
299300
}
@@ -311,11 +312,11 @@ export function getOwnPropertyNamesOptionsToFlags(
311312
}
312313

313314
const {
314-
includeStrings,
315-
includeSymbols,
316-
includePrivate,
315+
strings: includeStrings,
316+
symbols: includeSymbols,
317+
quickjsPrivate: includePrivate,
317318
onlyEnumerable,
318-
includeNumbers,
319+
numbers: includeNumbers,
319320
numbersAsStrings,
320321
} = options
321322
let flags = 0

packages/quickjs-emscripten/src/quickjs.test.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ function contextTests(getContext: GetTestContext, isDebug = false) {
408408
})
409409
})
410410

411-
describe("getPropNames", () => {
411+
describe("getOwnPropertyNames", () => {
412412
it("gets array indexes as *numbers*", ({ manage }) => {
413413
const array = manage(vm.newArray())
414414
vm.setProp(array, 0, vm.undefined)
@@ -417,8 +417,8 @@ function contextTests(getContext: GetTestContext, isDebug = false) {
417417

418418
const props = manage(
419419
vm
420-
.getPropNames(array, {
421-
includeNumbers: true,
420+
.getOwnPropertyNames(array, {
421+
numbers: true,
422422
})
423423
.unwrap(),
424424
)
@@ -440,12 +440,12 @@ function contextTests(getContext: GetTestContext, isDebug = false) {
440440
vm.setProp(obj, sym, vm.undefined)
441441

442442
const props = manage(
443-
vm.unwrapResult(
444-
vm.getPropNames(obj, {
443+
vm
444+
.getOwnPropertyNames(obj, {
445445
onlyEnumerable: true,
446-
includeStrings: true,
447-
}),
448-
),
446+
strings: true,
447+
})
448+
.unwrap(),
449449
)
450450

451451
assert.strictEqual(props.length, 3)
@@ -463,16 +463,35 @@ function contextTests(getContext: GetTestContext, isDebug = false) {
463463

464464
const props = manage(
465465
vm.unwrapResult(
466-
vm.getPropNames(obj, {
466+
vm.getOwnPropertyNames(obj, {
467467
onlyEnumerable: true,
468-
includeSymbols: true,
468+
symbols: true,
469469
}),
470470
),
471471
)
472472

473473
assert.strictEqual(props.length, 1)
474474
assert.strictEqual(vm.typeof(props[0]), "symbol")
475475
})
476+
477+
it("gets number keys as strings when in standard compliant mode", ({ manage }) => {
478+
const array = manage(vm.newArray())
479+
vm.setProp(array, 0, vm.undefined)
480+
vm.setProp(array, 1, vm.undefined)
481+
vm.setProp(array, 2, vm.undefined)
482+
vm.setProp(array, "dog", vm.undefined)
483+
const props = manage(
484+
vm.getOwnPropertyNames(array, {
485+
strings: true,
486+
numbersAsStrings: true,
487+
onlyEnumerable: true,
488+
}),
489+
)
490+
.unwrap()
491+
.map((p) => vm.dump(p))
492+
493+
assert.deepStrictEqual(["0", "1", "2", "dog"], props)
494+
})
476495
})
477496

478497
describe(".unwrapResult", () => {

0 commit comments

Comments
 (0)