Skip to content

Correctly clone existing active context - #43

Open
ashleyw wants to merge 1 commit into
Jeff-Lewis:masterfrom
ashleyw:patch-1
Open

Correctly clone existing active context#43
ashleyw wants to merge 1 commit into
Jeff-Lewis:masterfrom
ashleyw:patch-1

Conversation

@ashleyw

@ashleyw ashleyw commented Jun 3, 2019

Copy link
Copy Markdown

Hi,

From what I understand, nested contexts are permitted and values will be inherited, however within createContext it currently uses Object.create(), but that doesn't actually clone the active context (on Node v8.16.0 & v10.16.0):

let context = Object.create(this.active ? this.active : Object.prototype);

Where I believe it should be using Object.assign() to clone the object:

const context = this.active ? Object.assign({}, this.active) : {};

For example:

const active = { _ns_name: '__ictx__', id: 145, a: -1, b: 2, c: 3, d: 4 };

Object.create(active);
// => {}

Whereas:

const active = { _ns_name: '__ictx__', id: 145, a: -1, b: 2, c: 3, d: 4 };

Object.assign({}, active);
// => { _ns_name: '__ictx__', id: 145, a: -1, b: 2, c: 3, d: 4 }

Am I missing something, or is this a bug?

Thanks!

@sbuljac

sbuljac commented Oct 23, 2019

Copy link
Copy Markdown

@ashleyw Object.create will set this.active as a prototype of a newly created object context. When a property is not found directly on context it will be searched on its prototype, in this case, this.active.

const prot = { test: 5 };
const obj = Object.create(prot);
console.log(obj.test)
// 5

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.

2 participants