feat(runtime+jsruntime): Object.prototype methods on Buffer + require() namespace#983
Merged
Merged
Conversation
…stances
safer-buffer (and any transitive consumer via express) opens with
`for (key in buffer) { if (!buffer.hasOwnProperty(key)) continue; ... }`.
Two distinct gaps both surfaced as `buffer.hasOwnProperty is not a function`:
1. `dispatch_buffer_method` (perry-runtime) had no Object.prototype fallbacks
on Buffer instances. Add `hasOwnProperty`, `propertyIsEnumerable`,
`valueOf`, `toLocaleString`, `isPrototypeOf` arms before the
`_ => undefined` fallback. Index-key probes check `(*buf_ptr).length`
(Node spec: indexed bytes are own enumerable, `length` is inherited).
`is_buffer_method_name` is extended so a method-as-value read returns
a bound-method closure that routes back through the new arms.
2. `__perry_require_namespace` (perry-jsruntime) returned an ESM
module-namespace object with a null prototype, so `nsObj.hasOwnProperty`
threw. When the namespace has no prototype, copy enumerable own
properties into a plain `{}` so Object.prototype.* is reachable. Class
/ function values are copied by reference, so static members and
`.prototype` survive.
test-files/test_buffer_prototype_methods.ts matches Node byte-for-byte.
Express smoke test no longer hits the safer-buffer crash (downstream
send/util.inherits stub gap is tracked separately).
6608ba1 to
8b664b8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
hasOwnProperty/propertyIsEnumerable/valueOf/toLocaleString/isPrototypeOfarms todispatch_buffer_methodso safer-buffer'sfor (key in buf) { if (!buf.hasOwnProperty(key)) ... }probe resolves on Buffer instances.__perry_require_namespacesorequire('buffer').hasOwnProperty(...)works (the actual safer-buffer crash site — the receiver is the module, not a Buffer instance).is_buffer_method_nameso method-as-value reads (typeof buf.hasOwnProperty === 'function') return a bound-method closure that dispatches back through the new arms.Index-key probes for
hasOwnProperty(idx)/propertyIsEnumerable(idx)check the numeric / int32 / numeric-string key against(*buf_ptr).length, matching Node (indexed bytes are own enumerable;lengthis on the prototype).Direct follow-up to PR #973 (Date/Array/Object .constructor) and PR #978 (ramda prototype methods).
Test plan
test-files/test_buffer_prototype_methods.tsbyte-for-byte matchesnode --experimental-strip-types:/tmp/perry-expresssmoke (import express from 'express') — pre-fix crashed atsafer-buffer/safer.js:36:15withbuffer.hasOwnProperty is not a function; post-fix safer-buffer load succeeds (downstreamsendhits an unrelatedutil.inheritsstub gap, tracked separately).cargo build --release -p perry-runtime -p perry-stdlib -p perry -p perry-jsruntimeclean.