feat(libSceJsc): implement JSValueMakeString and JSObjectHasPropertyForKey #19271

Merged
c1b3r-p3d1a merged 1 commit from upstream-pr/19271 into main 2026-11-13 05:45:28 +00:00
c1b3r-p3d1a commented 2026-11-13 01:26:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by c1b3r-p3d1a on 2026-11-13T01:26:36Z.


What

Implements JSValueMakeString and JSObjectHasPropertyForKey, two JavaScriptCore C API exports that libSceJsc declares among its older (3.20) callable exports and that threw through NotImplemented_nid_no_patch.

libSceJsc exports them, but JS contexts and values exist only in libSceNKWebKit, which builds the C API on QuickJS (JSGlobalContextCreate, JSValueMakeNumber, JSObjectGetPropertyForKey and the rest). libSceNKWebKit already links libSceJsc, so the two exports cannot call into it directly. The PR uses the pattern libc uses for timespec_getres:

  • libSceJsc (src/ApiBridge.cpp) defines the exports and RegisterApiBridge_nid_no_patch, which stores a table of two function pointers.
  • libSceNKWebKit (src/JscApiBridge.cpp) registers its QuickJS implementations from a static initializer, as libkernel registers sceKernelClockGetres with libc.
  • A null context returns null or false before the table is read, as in WebKit. A call with a context while nothing is registered throws. That cannot happen in practice, because a context can only come from libSceNKWebKit.

Behaviour follows the C API in WebKit 73a35b43 (the revision used for other 3.20 exports), JSValueRef.cpp L336-L347 and JSObjectRef.cpp L386-L406:

  • JSValueMakeString makes a JS string from the JSStringRef (a null string makes "") and returns a handle like the other JSValueMake* functions.
  • JSObjectHasPropertyForKey converts the key with QuickJS's property-key conversion (JS_ValueToAtom, the counterpart of toPropertyKey) and asks JS_HasProperty, so the prototype chain and proxy has traps count. An exception from the key conversion or from a trap goes to exception and the call returns false. A null object or key returns false, as the existing libSceNKWebKit ForKey siblings do.

These are written from WebKit's documented behaviour on top of the existing QuickJS code; no WebKit code is copied. The "throwing JavaScriptCore, Inspector, WTF and bmalloc exports" count in docs/dev/debt/libSceJsc.md goes from 136 to 134. JSWeakObjectMapCreate stays a placeholder.

Unverified, as the debt entry says: nothing was compared with a console module.

Tested

Windows 11, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh r7), Release.

  • New nkwebkit_jscapibridge (in the NKWebKit tests, since only libSceNKWebKit creates contexts). For JSValueMakeString it checks non-ASCII text checked from script, empty and null strings, two calls giving strictly equal values, and a null context. For JSObjectHasPropertyForKey it checks string, number and symbol keys, inherited properties, a different symbol with the same description, proxy has traps that answer or throw, a key whose toString throws (with and without an exception pointer), and null context, object and key.
  • It fails with each of 9 mutations: a throwing body for each export, no null-context check, the table never registered, the string argument ignored, the result inverted, an exception dropped on key conversion or on a trap, and own-property-only lookup.
  • Disassembly of the built libSceJsc.prx: each export takes its arguments in rdi, rsi, rdx, rcx, returns 0 for a null context without touching the table, and otherwise calls the registered host function with the Windows host convention.
  • Based on main 066b1f8a41: all 146 nkwebkit, jsc and public_headers tests pass; python tools/check_conventions.py --base origin/main passes.
  • On Windows rebuilt libraries are not copied over the ones staged in build/tests (#17895), so I copied libSceJsc.prx and libSceNKWebKit.prx there before each run.
  • Not run: the full suite, Linux, a console.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Note: #19193 also lowers the libSceJsc "throwing exports" count. Git merges the two edits of that line without a conflict and keeps both lines, so whichever merges second must drop the stale line and recount; I will do that on rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5288291881 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19271). Originally posted by [c1b3r-p3d1a](https://github.com/c1b3r-p3d1a) on 2026-11-13T01:26:36Z. --- ### What Implements `JSValueMakeString` and `JSObjectHasPropertyForKey`, two JavaScriptCore C API exports that `libSceJsc` declares among its older (3.20) callable exports and that threw through `NotImplemented_nid_no_patch`. `libSceJsc` exports them, but JS contexts and values exist only in `libSceNKWebKit`, which builds the C API on QuickJS (`JSGlobalContextCreate`, `JSValueMakeNumber`, `JSObjectGetPropertyForKey` and the rest). `libSceNKWebKit` already links `libSceJsc`, so the two exports cannot call into it directly. The PR uses the pattern `libc` uses for `timespec_getres`: - `libSceJsc` (`src/ApiBridge.cpp`) defines the exports and `RegisterApiBridge_nid_no_patch`, which stores a table of two function pointers. - `libSceNKWebKit` (`src/JscApiBridge.cpp`) registers its QuickJS implementations from a static initializer, as `libkernel` registers `sceKernelClockGetres` with `libc`. - A null context returns null or false before the table is read, as in WebKit. A call with a context while nothing is registered throws. That cannot happen in practice, because a context can only come from `libSceNKWebKit`. Behaviour follows the C API in WebKit 73a35b43 (the revision used for other 3.20 exports), [JSValueRef.cpp L336-L347](https://github.com/WebKit/WebKit/blob/73a35b43be7ae44c084885bd3fa4b403e59723c8/Source/JavaScriptCore/API/JSValueRef.cpp#L336-L347) and [JSObjectRef.cpp L386-L406](https://github.com/WebKit/WebKit/blob/73a35b43be7ae44c084885bd3fa4b403e59723c8/Source/JavaScriptCore/API/JSObjectRef.cpp#L386-L406): - `JSValueMakeString` makes a JS string from the `JSStringRef` (a null string makes `""`) and returns a handle like the other `JSValueMake*` functions. - `JSObjectHasPropertyForKey` converts the key with QuickJS's property-key conversion (`JS_ValueToAtom`, the counterpart of `toPropertyKey`) and asks `JS_HasProperty`, so the prototype chain and proxy `has` traps count. An exception from the key conversion or from a trap goes to `exception` and the call returns false. A null object or key returns false, as the existing `libSceNKWebKit` `ForKey` siblings do. These are written from WebKit's documented behaviour on top of the existing QuickJS code; no WebKit code is copied. The "throwing JavaScriptCore, Inspector, WTF and bmalloc exports" count in `docs/dev/debt/libSceJsc.md` goes from 136 to 134. `JSWeakObjectMapCreate` stays a placeholder. Unverified, as the debt entry says: nothing was compared with a console module. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh r7), Release. - New `nkwebkit_jscapibridge` (in the NKWebKit tests, since only `libSceNKWebKit` creates contexts). For `JSValueMakeString` it checks non-ASCII text checked from script, empty and null strings, two calls giving strictly equal values, and a null context. For `JSObjectHasPropertyForKey` it checks string, number and symbol keys, inherited properties, a different symbol with the same description, proxy `has` traps that answer or throw, a key whose `toString` throws (with and without an `exception` pointer), and null context, object and key. - It fails with each of 9 mutations: a throwing body for each export, no null-context check, the table never registered, the string argument ignored, the result inverted, an exception dropped on key conversion or on a trap, and own-property-only lookup. - Disassembly of the built `libSceJsc.prx`: each export takes its arguments in `rdi`, `rsi`, `rdx`, `rcx`, returns 0 for a null context without touching the table, and otherwise calls the registered host function with the Windows host convention. - Based on `main` `066b1f8a41`: all 146 `nkwebkit`, `jsc` and `public_headers` tests pass; `python tools/check_conventions.py --base origin/main` passes. - On Windows rebuilt libraries are not copied over the ones staged in `build/tests` (#17895), so I copied `libSceJsc.prx` and `libSceNKWebKit.prx` there before each run. - Not run: the full suite, Linux, a console. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes Note: #19193 also lowers the `libSceJsc` "throwing exports" count. Git merges the two edits of that line without a conflict and keeps both lines, so whichever merges second must drop the stale line and recount; I will do that on rebase.
PrivacyOnion commented 2026-11-13 05:40:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-13T05:40:28Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against e00126ee6c:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: nkwebkit_jscapibridge_tests built cleanly.
  • CTest (nkwebkit_jscapibridge):
    1/1 Test #3845: nkwebkit_jscapibridge ............   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceJsc/Export.cpp, core/libs/prx/libSceJsc/include/Api.hpp, core/libs/prx/libSceJsc/libSceJsc.hpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • core/libs/prx/libSceJsc/src/ApiBridge.cpp: Verified JSValueMakeString and JSObjectHasPropertyForKey guard against null context pointers by returning nullptr and false respectively prior to dereferencing registeredBridge.
    • core/libs/prx/libSceJsc/src/ApiBridge.cpp: Verified std::atomic memory ordering (memory_order_release on RegisterApiBridge_nid_no_patch, memory_order_acquire on Bridge) and verified that unregistered invocation routes to NotImplemented_nid_no_patch.
    • core/libs/prx/libSceNKWebKit/src/JscApiBridge.cpp: Verified HasPropertyForKey releases the converted JSAtom with JS_FreeAtom(ctx->qctx, atom) on all execution paths, preventing atom leaks.
    • core/libs/prx/libSceNKWebKit/src/JscApiBridge.cpp: Verified exception propagation in HasPropertyForKey for property-key conversion failures (atom == JS_ATOM_NULL) and Proxy trap exceptions (result < 0), safely capturing via CapturePendingException even when exceptionRef is null.
    • core/libs/prx/libSceNKWebKit/src/JscApiBridge.cpp: Verified MakeString correctly handles null string pointers by delegating to JsStringToJsValue to produce an empty string, and frees pending exceptions on allocation failure.
    • AGENTS.md & docs/dev/debt/libSceJsc.md: Verified strict compliance with repository rules, including zero comments in C++ source/test files, proper debt accounting updating throwing exports from 136 to 134, and comprehensive test coverage across edge cases and mutations in tests/JscApiBridge.cpp.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5886701326 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19271#pullrequestreview-5886701326). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-13T05:40:28Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `e00126ee6c`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `nkwebkit_jscapibridge_tests` built cleanly. - **CTest** (`nkwebkit_jscapibridge`): ``` 1/1 Test #3845: nkwebkit_jscapibridge ............ Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceJsc/Export.cpp`, `core/libs/prx/libSceJsc/include/Api.hpp`, `core/libs/prx/libSceJsc/libSceJsc.hpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - core/libs/prx/libSceJsc/src/ApiBridge.cpp: Verified JSValueMakeString and JSObjectHasPropertyForKey guard against null context pointers by returning nullptr and false respectively prior to dereferencing registeredBridge. - core/libs/prx/libSceJsc/src/ApiBridge.cpp: Verified std::atomic memory ordering (memory_order_release on RegisterApiBridge_nid_no_patch, memory_order_acquire on Bridge) and verified that unregistered invocation routes to NotImplemented_nid_no_patch. - core/libs/prx/libSceNKWebKit/src/JscApiBridge.cpp: Verified HasPropertyForKey releases the converted JSAtom with JS_FreeAtom(ctx->qctx, atom) on all execution paths, preventing atom leaks. - core/libs/prx/libSceNKWebKit/src/JscApiBridge.cpp: Verified exception propagation in HasPropertyForKey for property-key conversion failures (atom == JS_ATOM_NULL) and Proxy trap exceptions (result < 0), safely capturing via CapturePendingException even when exceptionRef is null. - core/libs/prx/libSceNKWebKit/src/JscApiBridge.cpp: Verified MakeString correctly handles null string pointers by delegating to JsStringToJsValue to produce an empty string, and frees pending exceptions on allocation failure. - AGENTS.md & docs/dev/debt/libSceJsc.md: Verified strict compliance with repository rules, including zero comments in C++ source/test files, proper debt accounting updating throwing exports from 136 to 134, and comprehensive test coverage across edge cases and mutations in tests/JscApiBridge.cpp. Looks Good To Me
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!19271
No description provided.