quickjs-emscripten icon indicating copy to clipboard operation
quickjs-emscripten copied to clipboard

Inspect and iterate handles

Open justjake opened this issue 1 year ago • 0 comments

This PR is a large change aimed to make it easier to work values in the guest, primarily focused on reading guest objects and collections. There are also a major but hopefully non-breaking change to the SuccessOrFail result type returned by many operations

Collection & Iteration

Fixes https://github.com/justjake/quickjs-emscripten/issues/185

  • For objects and arrays: add context.getOwnPropertyNames(handle, options) to iterate the key or array index handles.
  • For arrays: add context.getLength(handle) which reads handle.length and returns it as a number or undefined to make writing for (i=0;i<length;i++) loops easier.
  • For iterable collections like Map, Set, Array: add context.getIterator(handle) calls handle[Symbol.iterator]() and then exposes the result as an IterableIterator to host javascript.

Usability improvements

  • The SuccessOrFail<T, QuickJSHandle> return type is largely replaced with a new return type DisposableSuccess<T> | DisposableFail<QuickJSHandle>. The new type implements result.unwrap() as a replacement for context.unwrapResult(result). It also implements dispose() directly, so you no longer need to distinguish between success and failure to clean up.
  • add context.callMethod(handle, 'methodName'), this makes it easier to call methods like context.callMethod(handle, 'keys') or context.callMethod('values') which can be used with the new iterator.

Equality

  • Added context.eq(a, b), context.sameValue(a, b), context.sameValueZero(a, b)

justjake avatar Aug 26 '24 23:08 justjake