Skip to content

Commit

Permalink
remove IsDataStatic
Browse files Browse the repository at this point in the history
  • Loading branch information
kvakil committed Mar 15, 2023
1 parent 6ff99f1 commit 8b9ee2b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
11 changes: 1 addition & 10 deletions src/node_builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -718,16 +718,7 @@ void BuiltinLoader::RegisterExternalReferences(
registry->Register(CompileFunction);
registry->Register(HasCachedBuiltins);

auto global_sources = GetGlobalSourceMap()->read();
for (auto const& x : *global_sources) {
// We can register any static data as an external reference, since (1) we
// know it'll survive until snapshot serialization or deserialization and
// (2) the list of static data is guaranteed to be in a fixed order (since
// resources with static data are internalized builtins created by js2c).
if (x.second.IsDataStatic()) {
registry->Register(x.second.AsResource());
}
}
RegisterExternalReferencesForInternalizedBuiltinCode(registry);
}

} // namespace builtins
Expand Down
4 changes: 3 additions & 1 deletion src/node_builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <set>
#include <string>
#include <vector>
#include "node_external_reference.h"
#include "node_mutex.h"
#include "node_threadsafe_cow.h"
#include "node_union_bytes.h"
Expand All @@ -31,7 +32,8 @@ using BuiltinCodeCacheMap =
std::unique_ptr<v8::ScriptCompiler::CachedData>>;

// Generated by tools/js2c.py as node_javascript.cc
const ThreadsafeCopyOnWrite<BuiltinSourceMap>* GetGlobalSourceMap();
void RegisterExternalReferencesForInternalizedBuiltinCode(
ExternalReferenceRegistry* registry);

struct CodeCacheInfo {
std::string id;
Expand Down
13 changes: 1 addition & 12 deletions src/node_union_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class StaticExternalByteResource : public Base {
}
size_t length() const override { return length_; }

bool IsDataStatic() const { return owning_ptr_ == nullptr; }

void Dispose() override {
// We ignore Dispose calls from V8, even if we "own" a resource via
// owning_ptr_. All instantiations of this class are static or owned by a
Expand Down Expand Up @@ -68,16 +66,7 @@ class UnionBytes {
UnionBytes& operator=(UnionBytes&&) = default;

bool IsOneByte() const { return one_byte_resource_ != nullptr; }
bool IsDataStatic() const {
return IsOneByte() ? one_byte_resource_->IsDataStatic()
: two_byte_resource_->IsDataStatic();
}
v8::String::ExternalStringResourceBase* AsResource() const {
return IsOneByte() ? static_cast<v8::String::ExternalStringResourceBase*>(
one_byte_resource_)
: static_cast<v8::String::ExternalStringResourceBase*>(
two_byte_resource_);
}

v8::Local<v8::String> ToStringChecked(v8::Isolate* isolate) const;

private:
Expand Down
23 changes: 16 additions & 7 deletions tools/js2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def ReadFile(filename):
TEMPLATE = """
#include "env-inl.h"
#include "node_builtins.h"
#include "node_external_reference.h"
#include "node_internals.h"
namespace node {{
Expand All @@ -67,12 +68,13 @@ def ReadFile(filename):
source_ = global_source_map;
}}
const ThreadsafeCopyOnWrite<BuiltinSourceMap> *GetGlobalSourceMap() {{
return &global_source_map;
void RegisterExternalReferencesForInternalizedBuiltinCode(
ExternalReferenceRegistry* registry) {{
{2}
}}
UnionBytes BuiltinLoader::GetConfig() {{
return UnionBytes(&{2});
return UnionBytes(&{3});
}}
}} // namespace builtins
Expand All @@ -98,6 +100,8 @@ def ReadFile(filename):

INITIALIZER = '{{"{0}", UnionBytes(&{1}) }},'

REGISTRATION = 'registry->Register(&{0});'

CONFIG_GYPI_ID = 'config_raw'

CONFIG_GYPI_RESOURCE_ID = 'config_resource'
Expand Down Expand Up @@ -130,16 +134,18 @@ def GetDefinition(var, source, resource_var, step=30):
return definition


def AddModule(filename, definitions, initializers):
def AddModule(filename, definitions, initializers, registrations):
code = ReadFile(filename)
name = NormalizeFileName(filename)
slug = SLUGGER_RE.sub('_', name)
var = slug + '_raw'
resource_var = slug + '_resource'
definition = GetDefinition(var, code, resource_var)
initializer = INITIALIZER.format(name, resource_var)
registration = REGISTRATION.format(resource_var)
definitions.append(definition)
initializers.append(initializer)
registrations.append(registration)

def NormalizeFileName(filename):
split = filename.split('/')
Expand All @@ -156,19 +162,22 @@ def JS2C(source_files, target):
# Build source code lines
definitions = []
initializers = []
registrations = []

for filename in source_files['.js']:
AddModule(filename, definitions, initializers)
AddModule(filename, definitions, initializers, registrations)
for filename in source_files['.mjs']:
AddModule(filename, definitions, initializers)
AddModule(filename, definitions, initializers, registrations)

config_def = handle_config_gypi(source_files['config.gypi'])
definitions.append(config_def)

# Emit result
definitions = ''.join(definitions)
initializers = '\n '.join(initializers)
out = TEMPLATE.format(definitions, initializers, CONFIG_GYPI_RESOURCE_ID)
registrations = '\n '.join(registrations)
out = TEMPLATE.format(definitions, initializers,
registrations, CONFIG_GYPI_RESOURCE_ID)
write_if_chaged(out, target)


Expand Down

0 comments on commit 8b9ee2b

Please sign in to comment.