From fe7945bca6032219ce90b03d4d2a379aafe08173 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Sun, 22 Nov 2026 21:24:53 -0300 Subject: [PATCH] fix(runtime): expose the effective native member guard state to JS The native member guard is on only in debug builds under the rooted and canary wrapper lifetime modes, unless allowShadowingNativeMembers is set. The specs derived it from the mode and that config alone, so a Release run of the suite under rooted or canary expected a guard the runtime leaves off. Add the read-only ns:runtime config key wrapperLifetimeGuardsNativeMembers, which reports GuardsNativeMembers(), and have NativeMemberGuardTests and WrapperLifetimeModesTests read it. A spec checks the key both ways against the mode, the shadowing config and the build (detected through the debug-only ns:module canonicalizeHttpUrlKey export). --- NativeScript/runtime/NsBuiltinModules.cpp | 9 ++++++- .../app/tests/NativeMemberGuardTests.js | 25 ++++++++++++++++--- .../app/tests/WrapperLifetimeModesTests.js | 6 ++--- docs/ns-builtin-modules.md | 5 +++- types/ns-runtime.d.ts | 7 ++++++ 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/NativeScript/runtime/NsBuiltinModules.cpp b/NativeScript/runtime/NsBuiltinModules.cpp index 3a96dece..842b72c9 100644 --- a/NativeScript/runtime/NsBuiltinModules.cpp +++ b/NativeScript/runtime/NsBuiltinModules.cpp @@ -110,6 +110,8 @@ constexpr const char* kWrapperLifetimeModeKey = "wrapperLifetimeMode"; constexpr const char* kWrapperLifetimeScopeKey = "wrapperLifetimeScope"; constexpr const char* kWrapperLifetimeAllowShadowingKey = "wrapperLifetimeAllowShadowingNativeMembers"; +constexpr const char* kWrapperLifetimeGuardsNativeMembersKey = + "wrapperLifetimeGuardsNativeMembers"; void ThrowTypeError(Isolate* isolate, const std::string& message) { isolate->ThrowException( @@ -149,7 +151,8 @@ void SetConfigCallback(const FunctionCallbackInfo& info) { return; } if (key == kWrapperLifetimeModeKey || key == kWrapperLifetimeScopeKey || - key == kWrapperLifetimeAllowShadowingKey) { + key == kWrapperLifetimeAllowShadowingKey || + key == kWrapperLifetimeGuardsNativeMembersKey) { ThrowTypeError( isolate, "'" + key + @@ -234,6 +237,10 @@ void GetConfigCallback(const FunctionCallbackInfo& info) { info.GetReturnValue().Set(tns::GetAllowShadowingNativeMembers()); return; } + if (key == kWrapperLifetimeGuardsNativeMembersKey) { + info.GetReturnValue().Set(tns::GuardsNativeMembers()); + return; + } ThrowTypeError(isolate, "Unknown runtime config key: '" + key + "'"); } diff --git a/TestRunner/app/tests/NativeMemberGuardTests.js b/TestRunner/app/tests/NativeMemberGuardTests.js index e9cf83e0..9d44924d 100644 --- a/TestRunner/app/tests/NativeMemberGuardTests.js +++ b/TestRunner/app/tests/NativeMemberGuardTests.js @@ -1,13 +1,12 @@ // Debug builds in the "rooted" and "canary" modes guard native instance // methods, and the methods of JS-extended classes that native code calls: // assigning one's name on a native object throws, since native code never -// calls that own property. The suite is a debug build; the resurrecting -// mode and "allowShadowingNativeMembers" keep plain data properties. +// calls that own property. Release builds, the resurrecting mode and +// "allowShadowingNativeMembers" keep plain data properties. describe("Native member guard", function () { var runtime = require("ns:runtime"); var mode = runtime.getConfig("wrapperLifetimeMode"); - var guarded = (mode === "rooted" || mode === "canary") && - !runtime.getConfig("wrapperLifetimeAllowShadowingNativeMembers"); + var guarded = runtime.getConfig("wrapperLifetimeGuardsNativeMembers"); function replacement() { return "js"; @@ -57,6 +56,24 @@ describe("Native member guard", function () { }).toThrowError(TypeError, /read-only/); }); + it("reports the guard state read-only", function () { + var allowShadowing = runtime.getConfig("wrapperLifetimeAllowShadowingNativeMembers"); + // canonicalizeHttpUrlKey is exported only when RuntimeConfig.IsDebug. + var debugBuild = typeof require("ns:module").canonicalizeHttpUrlKey === "function"; + expect(typeof guarded).toBe("boolean"); + if (guarded) { + expect(mode === "rooted" || mode === "canary").toBe(true); + expect(allowShadowing).toBe(false); + } + if (debugBuild && (mode === "rooted" || mode === "canary") && !allowShadowing) { + expect(guarded).toBe(true); + } + expect(function () { + runtime.setConfig("wrapperLifetimeGuardsNativeMembers", !guarded); + }).toThrowError(TypeError, /read-only/); + expect(runtime.getConfig("wrapperLifetimeGuardsNativeMembers")).toBe(guarded); + }); + it("guards native instance methods against strict and sloppy instance writes", function () { var object = NSObject.new(); var array = NSArray.arrayWithArray([1, 2]); diff --git a/TestRunner/app/tests/WrapperLifetimeModesTests.js b/TestRunner/app/tests/WrapperLifetimeModesTests.js index 8eabd801..d68a2b06 100644 --- a/TestRunner/app/tests/WrapperLifetimeModesTests.js +++ b/TestRunner/app/tests/WrapperLifetimeModesTests.js @@ -9,9 +9,9 @@ describe("Wrapper lifetime modes", function () { var canary = mode === "canary"; // JS state on a natively owned object survives its wrapper's death. var keepsState = rooted || canary; - // The suite is a debug build: assigning a native method's name on a native - // object throws in these modes (the native member guard). - var guardsNativeMembers = keepsState && !runtime.getConfig("wrapperLifetimeAllowShadowingNativeMembers"); + // Assigning a native method's name on a native object throws (the native + // member guard; debug builds in these modes only). + var guardsNativeMembers = runtime.getConfig("wrapperLifetimeGuardsNativeMembers"); var Extended = (function (_super) { __extends(Extended, _super); diff --git a/docs/ns-builtin-modules.md b/docs/ns-builtin-modules.md index 930f3b79..f64e5a43 100644 --- a/docs/ns-builtin-modules.md +++ b/docs/ns-builtin-modules.md @@ -121,6 +121,7 @@ Config keys: | `wrapperLifetimeMode` | `"resurrecting"` \| `"rooted"` \| `"canary"` | process-wide, read-only | the app config `"wrapperLifetime": { "mode": … }`, or `"resurrecting"` | | `wrapperLifetimeScope` | `"extended"` \| `"all"` | process-wide, read-only | the app config `"wrapperLifetime": { "scope": … }`, or `"extended"` | | `wrapperLifetimeAllowShadowingNativeMembers` | `true` \| `false` | process-wide, read-only | the app config `"wrapperLifetime": { "allowShadowingNativeMembers": … }`, or `false` (see [Native member guard](#native-member-guard) and [Plain canaries](#plain-canaries)) | +| `wrapperLifetimeGuardsNativeMembers` | `true` \| `false` | process-wide, read-only | `true` in debug builds under the `"rooted"` and `"canary"` modes unless `wrapperLifetimeAllowShadowingNativeMembers` is `true`, else `false` (see [Native member guard](#native-member-guard)) | ```js const { setConfig, getConfig } = require("ns:runtime"); @@ -149,7 +150,7 @@ The `TypeError` messages are part of the contract: | invalid `releasedObjectPolicy` value | `'releasedObjectPolicy' must be 'report' or 'throw'` | | non-string `debug` value | `'debug' must be a comma-separated category string (), or '' to disable tracing` | | non-boolean `wrapperLifetimeWarnings` value | `'wrapperLifetimeWarnings' must be a boolean` | -| `setConfig` of `wrapperLifetimeMode`, `wrapperLifetimeScope` or `wrapperLifetimeAllowShadowingNativeMembers` | `'' is read-only; set "wrapperLifetime" in the app config` | +| `setConfig` of `wrapperLifetimeMode`, `wrapperLifetimeScope`, `wrapperLifetimeAllowShadowingNativeMembers` or `wrapperLifetimeGuardsNativeMembers` | `'' is read-only; set "wrapperLifetime" in the app config` | ### Wrapper lifetime counters @@ -349,6 +350,8 @@ mirroring handler, so a shadowing property on one carries over to a new wrapper as it does on a JS-extended instance, at the cost of a handler on every write to a plain wrapper (see [Plain canaries](#plain-canaries)); `getConfig("wrapperLifetimeAllowShadowingNativeMembers")` reads it. +`getConfig("wrapperLifetimeGuardsNativeMembers")` reads whether the guard is +on, which also accounts for the build and the mode. Release builds never throw and keep data properties, since the accessor costs every native method load. An instance shadow there is silently ignored by diff --git a/types/ns-runtime.d.ts b/types/ns-runtime.d.ts index dee337de..35c74493 100644 --- a/types/ns-runtime.d.ts +++ b/types/ns-runtime.d.ts @@ -107,6 +107,13 @@ declare module "ns:runtime" { * handler on every write. */ wrapperLifetimeAllowShadowingNativeMembers: boolean; + /** + * Whether assigning a native member's name on a native object throws (see + * `wrapperLifetimeAllowShadowingNativeMembers`): `true` in debug builds + * under the `"rooted"` and `"canary"` modes unless shadowing is allowed, + * `false` otherwise. Fixed at startup. Read-only: `setConfig` throws. + */ + wrapperLifetimeGuardsNativeMembers: boolean; } /**