From 4d5011a5b320e73ca646f4ef7eaa5f86c1c562ec Mon Sep 17 00:00:00 2001 From: Thorsten Lorenz Date: Tue, 14 Jul 2015 12:57:42 -0400 Subject: [PATCH 1/2] bindings: adding process.binding('builtins') method - this method traverses the `modlist_builtin` linked list and returns an object which has binding names as keys and their versions as values --- node.gyp | 2 ++ src/node.cc | 5 +++++ src/node_builtins.cc | 28 ++++++++++++++++++++++++++++ src/node_builtins.h | 15 +++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 src/node_builtins.cc create mode 100644 src/node_builtins.h diff --git a/node.gyp b/node.gyp index dfa08ce6468f30..1133a19ce9db6d 100644 --- a/node.gyp +++ b/node.gyp @@ -109,6 +109,7 @@ 'src/js_stream.cc', 'src/node.cc', 'src/node_buffer.cc', + 'src/node_builtins.cc', 'src/node_constants.cc', 'src/node_contextify.cc', 'src/node_file.cc', @@ -146,6 +147,7 @@ 'src/js_stream.h', 'src/node.h', 'src/node_buffer.h', + 'src/node_builtins.h', 'src/node_constants.h', 'src/node_file.h', 'src/node_http_parser.h', diff --git a/src/node.cc b/src/node.cc index 82d081ce17aa90..d9343aa7083a72 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1,5 +1,6 @@ #include "node.h" #include "node_buffer.h" +#include "node_builtins.h" #include "node_constants.h" #include "node_file.h" #include "node_http_parser.h" @@ -2281,6 +2282,10 @@ static void Binding(const FunctionCallbackInfo& args) { exports = Object::New(env->isolate()); DefineJavaScript(env, exports); cache->Set(module, exports); + } else if (!strcmp(*module_v, "builtins")) { + exports = Object::New(env->isolate()); + DefineBuiltins(env, exports, modlist_builtin); + cache->Set(module, exports); } else { char errmsg[1024]; snprintf(errmsg, diff --git a/src/node_builtins.cc b/src/node_builtins.cc new file mode 100644 index 00000000000000..faf5dac6c23627 --- /dev/null +++ b/src/node_builtins.cc @@ -0,0 +1,28 @@ +#include "node.h" +#include "v8.h" +#include "env.h" +#include "env-inl.h" + +namespace node { + +using v8::Handle; +using v8::HandleScope; +using v8::Integer; +using v8::Local; +using v8::Object; +using v8::String; + +void DefineBuiltins(Environment* env, + Handle target, + node_module* builtins) { + HandleScope scope(env->isolate()); + struct node_module* mp; + + for (mp = builtins; mp != nullptr; mp = mp->nm_link) { + Local name = String::NewFromUtf8(env->isolate(), mp->nm_modname); + Local version = Integer::New(env->isolate(), mp->nm_version); + target->Set(name, version); + } +} + +} // namespace node diff --git a/src/node_builtins.h b/src/node_builtins.h new file mode 100644 index 00000000000000..1a9ffb4584c70e --- /dev/null +++ b/src/node_builtins.h @@ -0,0 +1,15 @@ +#ifndef SRC_NODE_BUILTINS_H_ +#define SRC_NODE_BUILTINS_H_ + +#include "v8.h" +#include "env.h" + +namespace node { + +void DefineBuiltins(Environment* env, + v8::Handle target, + node_module* builtins); + +} // namespace node + +#endif // SRC_NODE_BUILTINS_H_ From 5651e0cc5c781db44bd72ee6e13abb33ac47c9a5 Mon Sep 17 00:00:00 2001 From: Thorsten Lorenz Date: Wed, 15 Jul 2015 11:12:46 -0400 Subject: [PATCH 2/2] fixup! bindings: adding process.binding('builtins') method --- src/node_builtins.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/node_builtins.cc b/src/node_builtins.cc index faf5dac6c23627..39511c2d73c390 100644 --- a/src/node_builtins.cc +++ b/src/node_builtins.cc @@ -5,6 +5,7 @@ namespace node { +using v8::Array; using v8::Handle; using v8::HandleScope; using v8::Integer; @@ -17,12 +18,14 @@ void DefineBuiltins(Environment* env, node_module* builtins) { HandleScope scope(env->isolate()); struct node_module* mp; + int i = 0; + Local bindings = Array::New(env->isolate(), 0); for (mp = builtins; mp != nullptr; mp = mp->nm_link) { Local name = String::NewFromUtf8(env->isolate(), mp->nm_modname); - Local version = Integer::New(env->isolate(), mp->nm_version); - target->Set(name, version); + bindings->Set(i++, name); } + target->Set(String::NewFromUtf8(env->isolate(), "bindings"), bindings); } } // namespace node