★ wanayoo — archive 1999 https://github.com/nodejs/node/pull/33139Nouvelle recherche | Portail wanayoo
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: retrieve binding data from the context #33139

Closed
wants to merge 5 commits into from

Conversation

addaleax
Copy link
Member

@addaleax addaleax commented Apr 29, 2020

This is mostly taken from the common subset of #32761 and #32984, with a few modifications on top of it (notably, BindingData does not make sense as a class any longer and can be replaced by BaseObject directly).


Instead of passing them through the data bound to function
templates, store references to them in a list embedded inside
the context, and store the integer index
(which is context-independent) in the function template data.
This makes the function templates more context-independent,
and makes it possible to embed binding data in non-main contexts.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added c++ lib / src labels Apr 29, 2020
@addaleax addaleax force-pushed the context-indepedent-bindings branch from c4e9967 to b6aaf21 Compare Apr 29, 2020
@addaleax addaleax requested a review from joyeecheung Apr 29, 2020
@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented Apr 29, 2020

src/env-inl.h Outdated
return v8::MaybeLocal<v8::Object>();
}
T* data = new T(this, obj);
inline std::pair<T*, uint32_t> Environment::NewBindingData(
Copy link
Member

@joyeecheung joyeecheung Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was the BindingDataBase abstraction removed? I think it's beneficial to have a common base class for them (for one, they share the characteristic that they are one-per-context, so for example we can add some checks for that, or do bookkeeping otherwise)

Copy link
Member Author

@addaleax addaleax Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung Because the class doesn’t currently add any extra value – it just forwards directly to BaseObject without functionality on its own. The only reason that my PR originally introduced it was to store the v8::External associated with it, but this PR is rendering that unnecessary.

If we do run into a situation in which we want to add something here it, adding an intermediate class should be straightforward.

@bnoordhuis
Copy link
Member

@bnoordhuis bnoordhuis commented Apr 29, 2020

This makes the function templates more context-independent, and makes it possible to embed binding data in non-main contexts.

Can you elaborate? I either don't understand:

  1. what currently doesn't work with non-main contexts, or
  2. why that is useful if "non-main" means "non-node"

src/env-inl.h Outdated
context->GetAlignedPointerFromEmbedderData(
ContextEmbedderIndex::kBindingListIndex));
DCHECK_NOT_NULL(list);
size_t index = list->size();
Copy link
Member

@joyeecheung joyeecheung Apr 29, 2020 •

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use a map to store these bindings, and add static methods to all the BindingDataBase subclass to return the same name as their internalBinding() identifier. Then we can just get the bindings via a look up using T::binding_name() as key and there will be no need for BindingScope (the binding initializers can make sure a pair of T::binding_name(), new T(env, target) is inserted into the map) or adding any data parameter to the Function Templates. Also then there's no need for the default callback data (because we'll just get the Environment from the Environment slot).

If we just use unordered map the lookup overhead is constant like std::vector anyways (but it probably doesn't matter that much given the size of this map)

Copy link
Member Author

@addaleax addaleax Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung No strong feelings here, and happy to make the switch if you think that should happen in this PR.

Copy link
Member

@joyeecheung joyeecheung Apr 30, 2020 •

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about doing changes like this to my patch before I open a PR myself, so I guess yes I'd like to see it happen here as I saw the old patch as incomplete.

Copy link
Member Author

@addaleax addaleax May 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung Done, including the removal of NoBindingData and BindingScope 🙂 PTAL

@joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Apr 29, 2020

@bnoordhuis Currently the function templates are created with a v8::Object as its attached data, and v8::Objects are context-dependent (because they reference the Object constructor of one particular context through the prototype chain), and need to go into the context snapshot, whereas function templates are context-independent and goes into the isolate snapshot - then attaching context-dependent objects to context-independent templates makes the templates non-snapshottable.

why that is useful if "non-main" means "non-node"

The idea is to make it possible to create "non-main" contexts with their Node.js builtins - that is, "non-main but node" contexts

joyeecheung and others added 3 commits May 5, 2020
Instead of passing them through the data bound to function
templates, store references to them in a list embedded inside
the context, and store the integer index
(which is context-independent) in the function template data.
This makes the function templates more context-independent,
and makes it possible to embed binding data in non-main contexts.
Co-authored-by: Anna Henningsen <anna@addaleax.net>
@addaleax addaleax force-pushed the context-indepedent-bindings branch from b6aaf21 to 2ff3173 Compare May 5, 2020
@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented May 5, 2020

@addaleax addaleax added the review wanted label May 5, 2020
@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented May 6, 2020

jasnell
jasnell approved these changes May 6, 2020
Copy link
Member

@joyeecheung joyeecheung left a comment

LGTM % nits, thanks

src/env-inl.h Outdated Show resolved Hide resolved
src/env-inl.h Outdated Show resolved Hide resolved
src/README.md Outdated Show resolved Hide resolved
src/node_env_var.cc Outdated Show resolved Hide resolved
@addaleax addaleax added author ready and removed review wanted labels May 6, 2020
@nodejs-github-bot
Copy link
Contributor

@nodejs-github-bot nodejs-github-bot commented May 6, 2020

addaleax added a commit that referenced this issue May 6, 2020
Instead of passing them through the data bound to function
templates, store references to them in a list embedded inside
the context.
This makes the function templates more context-independent,
and makes it possible to embed binding data in non-main contexts.

Co-authored-by: Anna Henningsen <anna@addaleax.net>

PR-URL: #33139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
@addaleax
Copy link
Member Author

@addaleax addaleax commented May 6, 2020

Landed in 86fdaa7

@addaleax addaleax closed this May 6, 2020
@addaleax addaleax deleted the context-indepedent-bindings branch May 6, 2020
codebytere added a commit that referenced this issue May 7, 2020
Instead of passing them through the data bound to function
templates, store references to them in a list embedded inside
the context.
This makes the function templates more context-independent,
and makes it possible to embed binding data in non-main contexts.

Co-authored-by: Anna Henningsen <anna@addaleax.net>

PR-URL: #33139
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
@codebytere codebytere mentioned this pull request May 18, 2020
@codebytere
Copy link
Member

@codebytere codebytere commented Jun 7, 2020 •

@addaleax there are a lot of conflict for this on 12.x - could you please open a manual backport?

@codebytere codebytere added the backport-requested-v12.x label Jun 7, 2020
@joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Jun 8, 2020

⚠ The following ancestor commits of 86fdaa7 are not on v12.x-staging

  • 6d6de56 src,doc: add documentation for per-binding state pattern, #32538
    src/README.md
  • f4c2dff src: move fs state out of Environment, #32538
    src/env-inl.h
    src/env.cc
    src/env.h
    src/node_file-inl.h
    src/node_file.cc
    src/node_file.h
    src/node_stat_watcher.cc
  • 1216e8f src: move http parser state out of Environment, #32538
    src/env-inl.h
    src/env.cc
    src/env.h
    src/node_http_parser.cc
  • c47d042 src: move v8 stats buffers out of Environment, #32538
    src/env-inl.h
    src/env.h
    src/node_v8.cc
  • f54b5b2 src: move HTTP/2 state out of Environment, #32538
    src/env-inl.h
    src/env.h
    src/node_http2.cc
    src/node_http2_state.h
  • 6576b9b src: make creating per-binding data structures easier, #32538
    src/env-inl.h
    src/env.cc
    src/env.h
    src/fs_event_wrap.cc
    src/node.cc
    src/node_crypto.cc
    src/node_process_object.cc
    src/stream_wrap.cc
    src/tls_wrap.cc
    src/udp_wrap.cc
  • d812f16 embedding: provide hook for custom process.exit() behaviour, #32531
    src/env-inl.h
  • 60c4c2b src: runtime deprecate process.umask(), #32499
    src/env-inl.h
  • a173473 src: flush V8 interrupts from Environment dtor, #32523
    src/env.cc
  • c664214 src: make Environment::interrupt_data_ atomic, #32523
    src/env.cc
    src/env.h
  • e629366 src,test: add regression test for nested Worker termination, #32623
    src/env.cc
  • f621536 Revert "embedding: make Stop() stop Workers", #32623
    src/env.cc
    src/env.h
    src/node.cc
  • 6f9f546 src: use env->RequestInterrupt() for inspector MainThreadInterface, #32523
    src/env.h
  • 037ac99 embedding: make Stop() stop Workers, #32531
    src/node.cc
  • c44edec src: provide a variant of LoadEnvironment taking a callback, #30467
    src/node.cc
  • a9fb51f src: align worker and main thread code with embedder API, #30467
    src/node.cc
  • ac59dc4 http: remove legacy parser, #29589
    src/node_binding.cc
    src/node_http_parser.cc
  • 47c2b67 src: DRY crypto Update() methods, #31767
    src/node_crypto.cc
  • 3b9a403 crypto: optimize sign.update() and verify.update(), #31767
    src/node_crypto.cc
  • 1b9a62c crypto: make DH error messages consistent, #31873
    src/node_crypto.cc
  • ee9280a http2,doc: minor fixes, #28044
    src/node_http2.cc
  • 654c0ac src: fix compiler warnings in node_http2.cc, #33014
    src/node_http2.cc
  • 91ca221 http2: refactor and cleanup http2, #32884
    src/node_http2.cc
  • cecb08f src: add AliasedStruct utility, #32778
    src/node_http2.cc
  • 0be9ebb src: minor http2 refactorings, #32551
    src/node_http2.cc
  • 5f5d380 src: rename http2 class and suppress compile warnings, #32551
    src/node_http2.cc
  • 8f8bbc6 src: use smart pointers for nghttp2 objects, #32551
    src/node_http2.cc
  • ffdf1de src: clean up stream_base.h and stream-base-inl.h, #32307
    src/node_http2.cc
    src/stream_wrap.cc
    src/tls_wrap.cc
  • 434d39d src,http2: introduce node_http_common, #32069
    src/node_http2.cc
  • 41637a5 http2: remove callback-based padding, #29144
    src/node_http2_state.h
  • b149eef http: fix incorrect headersTimeout measurement, #32329
    src/node_http_parser.cc
  • 6bf5a1d http: make maximum header size configurable per-stream or per-server, #30570
    src/node_http_parser.cc
  • 7dead84 src: add LoadEnvironment() variant taking a string, #30467
    src/node_native_module_env.cc
  • aa9708e v8: use AliasedBuffers for passing heap statistics around, #32929
    src/node_v8.cc
  • effebf8 src: remove unused v8 namespace, #32375
    src/node_v8.cc
  • d06efaf src: explicitly allocate backing stores for v8 stat buffers, #30946
    src/node_v8.cc
  • 4f523c2 src: migrate to new V8 ArrayBuffer API, #30782
    src/node_v8.cc
  • e66a2ac src: migrate off ArrayBuffer::GetContents, #30339
    src/util-inl.h
    src/util.h
  • 0bbda5e fs: allow int64 offset in fs.read/readSync/fd.read, #26572
    src/util-inl.h
    src/util.h

FYI these are the missing ancestor commits git node backport found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready backport-requested-v12.x c++ lib / src
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants