summaryrefslogtreecommitdiff
path: root/src/node_union_bytes.h
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2018-11-14 22:38:12 +0800
committerJoyee Cheung <joyeec9h3@gmail.com>2018-11-20 01:17:15 +0800
commit7778c035a0a3f9bbb988eb0298a026d80f423e03 (patch)
treed3c80bffa25e84ae9993aaf0314192c2ac4f969f /src/node_union_bytes.h
parent092ab7a1d39d97a3e93f8ed46d80d8ad783b2f9c (diff)
downloadandroid-node-v8-7778c035a0a3f9bbb988eb0298a026d80f423e03.tar.gz
android-node-v8-7778c035a0a3f9bbb988eb0298a026d80f423e03.tar.bz2
android-node-v8-7778c035a0a3f9bbb988eb0298a026d80f423e03.zip
src: use STL containers instead of v8 values for static module data
Instead of putting the source code and the cache in v8::Objects, put them in per-process std::maps. This has the following benefits: - It's slightly lighter in weight compared to storing things on the v8 heap. Also it may be slightly faster since the preivous v8::Object is already in dictionary mode - though the difference is very small given the number of native modules is limited. - The source and code cache generation templates are now much simpler since they just initialize static arrays and manipulate STL constructs. - The static native module data can be accessed independently of any Environment or Isolate, and it's easy to look them up from the C++'s side. - It's now impossible to mutate the source code used to compile native modules from the JS land since it's completely separate from the v8 heap. We can still get the constant strings from process.binding('natives') but that's all. A few drive-by fixes: - Remove DecorateErrorStack in LookupAndCompile - We don't need to capture the exception to decorate when we encounter errors during native module compilation, as those errors should be syntax errors and v8 is able to decorate them well. We use CompileFunctionInContext so there is no need to worry about wrappers either. - The code cache could be rejected when node is started with v8 flags. Instead of aborting in that case, simply keep a record in the native_module_without_cache set. - Refactor js2c.py a bit, reduce code duplication and inline Render() to make the one-byte/two-byte special treatment easier to read. PR-URL: https://github.com/nodejs/node/pull/24384 Fixes: https://github.com/Remove Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_union_bytes.h')
-rw-r--r--src/node_union_bytes.h93
1 files changed, 93 insertions, 0 deletions
diff --git a/src/node_union_bytes.h b/src/node_union_bytes.h
new file mode 100644
index 0000000000..128bb11ed0
--- /dev/null
+++ b/src/node_union_bytes.h
@@ -0,0 +1,93 @@
+
+#ifndef SRC_NODE_UNION_BYTES_H_
+#define SRC_NODE_UNION_BYTES_H_
+
+#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
+
+// A union of const uint8_t* or const uint16_t* data that can be
+// turned into external v8::String when given an isolate.
+
+#include "env.h"
+#include "v8.h"
+
+namespace node {
+
+class NonOwningExternalOneByteResource
+ : public v8::String::ExternalOneByteStringResource {
+ public:
+ explicit NonOwningExternalOneByteResource(const uint8_t* data, size_t length)
+ : data_(data), length_(length) {}
+ ~NonOwningExternalOneByteResource() override = default;
+
+ const char* data() const override {
+ return reinterpret_cast<const char*>(data_);
+ }
+ size_t length() const override { return length_; }
+
+ private:
+ const uint8_t* data_;
+ size_t length_;
+ DISALLOW_COPY_AND_ASSIGN(NonOwningExternalOneByteResource);
+};
+
+class NonOwningExternalTwoByteResource
+ : public v8::String::ExternalStringResource {
+ public:
+ explicit NonOwningExternalTwoByteResource(const uint16_t* data, size_t length)
+ : data_(data), length_(length) {}
+ ~NonOwningExternalTwoByteResource() override = default;
+
+ const uint16_t* data() const override { return data_; }
+ size_t length() const override { return length_; }
+
+ private:
+ const uint16_t* data_;
+ size_t length_;
+ DISALLOW_COPY_AND_ASSIGN(NonOwningExternalTwoByteResource);
+};
+
+// Similar to a v8::String, but it's independent from Isolates
+// and can be materialized in Isolates as external Strings
+// via ToStringChecked. The data pointers are owned by the caller.
+class UnionBytes {
+ public:
+ UnionBytes(const uint16_t* data, size_t length)
+ : is_one_byte_(false), two_bytes_(data), length_(length) {}
+ UnionBytes(const uint8_t* data, size_t length)
+ : is_one_byte_(true), one_bytes_(data), length_(length) {}
+ bool is_one_byte() const { return is_one_byte_; }
+ const uint16_t* two_bytes_data() const {
+ CHECK(!is_one_byte_);
+ return two_bytes_;
+ }
+ const uint8_t* one_bytes_data() const {
+ CHECK(is_one_byte_);
+ return one_bytes_;
+ }
+ v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const {
+ if (is_one_byte_) {
+ NonOwningExternalOneByteResource* source =
+ new NonOwningExternalOneByteResource(one_bytes_, length_);
+ return v8::String::NewExternalOneByte(isolate, source).ToLocalChecked();
+ } else {
+ NonOwningExternalTwoByteResource* source =
+ new NonOwningExternalTwoByteResource(two_bytes_, length_);
+ return v8::String::NewExternalTwoByte(isolate, source).ToLocalChecked();
+ }
+ }
+ size_t length() { return length_; }
+
+ private:
+ bool is_one_byte_;
+ union {
+ const uint8_t* one_bytes_;
+ const uint16_t* two_bytes_;
+ };
+ size_t length_;
+};
+
+} // namespace node
+
+#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
+
+#endif // SRC_NODE_UNION_BYTES_H_