Skip to content

fix(codegen): typeof of native-module methods is "function" (#1343) - #1415

Merged
proggeramlug merged 1 commit into
mainfrom
fix/native-method-typeof-1343
May 22, 2026
Merged

fix(codegen): typeof of native-module methods is "function" (#1343)#1415
proggeramlug merged 1 commit into
mainfrom
fix/native-method-typeof-1343

Conversation

@proggeramlug

Copy link
Copy Markdown
Contributor

typeof crypto.randomBytes (and any native-module method read as a value, not called) returned "undefined". The member is only addressable through the call-dispatch arms, so reading it as a plain value yields the module's 0.0 stub, which js_value_typeof classifies as undefined/number.

Fix

Add a NativeModuleRef arm to the typeof short-circuit in expr/literals_vars.rs: look the member up in the API manifest (perry_api_manifest::module_has_symbol) and emit "function" for Method/Class entries. Properties fall through (None) so their materialized value types correctly — process.pid stays "number", os.EOL "string", crypto.constants "object".

General fix: any manifest-registered native method now reports "function". Covers crypto.* today; process.* methods benefit automatically once they're registered (currently several aren't, which is a separate gap — they hit the #463 gate before typeof).

Validation

Byte-for-byte vs node --experimental-strip-types:

  • typeof of randomBytes / createHash / randomUUID / pbkdf2Sync / scryptSync / timingSafeEqual / getHashes"function"
  • typeof crypto.constants"object" (property, falls through)
  • regression: typeof crypto.randomUUID()"string", typeof crypto.randomBytes(4).toString('hex')"string"

Codegen-only; no manifest/runtime change.

Closes #1343

typeof crypto.randomBytes (and any native-module method read as a value)
returned "undefined": the member is only addressable through the call-
dispatch arms, so reading it as a plain value yields the module's 0.0 stub,
which js_value_typeof classifies as undefined/number.

Add a NativeModuleRef arm to the typeof short-circuit in literals_vars.rs:
look the member up in the API manifest and emit "function" for Method/Class
entries. Properties fall through (None) so their materialized value types
correctly — process.pid stays "number", os.EOL "string", crypto.constants
"object". General fix: any manifest-registered native method now reports
"function" (crypto.*; process.* once those methods are registered).

Verified vs `node --experimental-strip-types`: typeof of randomBytes /
createHash / randomUUID / pbkdf2Sync / scryptSync / timingSafeEqual /
getHashes is "function", crypto.constants is "object", and call-result /
string regressions are unchanged.

Closes #1343
@proggeramlug
proggeramlug force-pushed the fix/native-method-typeof-1343 branch from 96d0697 to 988494b Compare May 22, 2026 18:51
@proggeramlug
proggeramlug merged commit 67985dc into main May 22, 2026
16 of 17 checks passed
@proggeramlug
proggeramlug deleted the fix/native-method-typeof-1343 branch May 22, 2026 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

node builtin module method values report wrong typeof (process.*, crypto.*)

1 participant