Fix NativesCollection<.>::GetScriptName in natives-external.cc
authorvogelheim <vogelheim@chromium.org>
Tue, 2 Dec 2014 18:53:27 +0000 (10:53 -0800)
committerCommit bot <commit-bot@chromium.org>
Tue, 2 Dec 2014 18:53:34 +0000 (18:53 +0000)
As there's no associated bug, here's the issue:
- Some ES6 functionality in Chrome is presently broken; this fixes it.
- The natives (built-in libraries) can be accessed by their 'name'.
  This is used to active ES6 flags.
- Strangely enough, there's an id and a name, where the name is derived
  from the id as "native %s.js", with %s for the id.
- NativesCollection<.>::GetScriptName uses the name.
- NativesCollection<.>::GetIndex uses the id.
- Example:
  NativesCollection<EXPERIMENTAL>::GetIndex("harmony-string") -> 3
  NativesCollection<EXPERIMENTAL>::GetScriptName(3) -> "native harmony-string.js"
- Nobody knows why; it's quite mysterious.
- When introducing the "external startup data", I didn't fully understand this
  and used the id in both places.
- When the "external startup data" was turned on in Chrome, ES6 features broke
  in Chrome since the libraries could no longer be found.
- This CL fixes this and makes the external startup data behave just like the
  built-in version.

R=dslomov
BUG=

Review URL: https://codereview.chromium.org/774613003

Cr-Commit-Position: refs/heads/master@{#25624}

src/bootstrapper.cc
src/natives-external.cc

index 052d9f627fdb75949ed1f523b9fb3abf437f12d2..53733ff3f534107022d288ea44973628c24e4bf1 100644 (file)
@@ -2151,14 +2151,6 @@ bool Genesis::InstallNatives() {
 }
 
 
-#define INSTALL_EXPERIMENTAL_NATIVE(i, flag, file)                             \
-  if (FLAG_##flag &&                                                           \
-      strcmp(ExperimentalNatives::GetScriptName(i).start(), "native " file) == \
-          0) {                                                                 \
-    if (!CompileExperimentalBuiltin(isolate(), i)) return false;               \
-  }
-
-
 bool Genesis::InstallExperimentalNatives() {
   static const char* harmony_arrays_natives[] = {
       "native harmony-array.js", "native harmony-typedarray.js", NULL};
@@ -2182,14 +2174,15 @@ bool Genesis::InstallExperimentalNatives() {
 
   for (int i = ExperimentalNatives::GetDebuggerCount();
        i < ExperimentalNatives::GetBuiltinsCount(); i++) {
-#define INSTALL_EXPERIMENTAL_NATIVES(id, desc)                       \
-  if (FLAG_##id) {                                                   \
-    for (size_t j = 0; id##_natives[j] != NULL; j++) {               \
-      if (strcmp(ExperimentalNatives::GetScriptName(i).start(),      \
-                 id##_natives[j]) == 0) {                            \
-        if (!CompileExperimentalBuiltin(isolate(), i)) return false; \
-      }                                                              \
-    }                                                                \
+#define INSTALL_EXPERIMENTAL_NATIVES(id, desc)                                \
+  if (FLAG_##id) {                                                            \
+    for (size_t j = 0; id##_natives[j] != NULL; j++) {                        \
+      Vector<const char> script_name = ExperimentalNatives::GetScriptName(i); \
+      if (strncmp(script_name.start(), id##_natives[j],                       \
+                  script_name.length()) == 0) {                               \
+        if (!CompileExperimentalBuiltin(isolate(), i)) return false;          \
+      }                                                                       \
+    }                                                                         \
   }
     HARMONY_INPROGRESS(INSTALL_EXPERIMENTAL_NATIVES);
     HARMONY_STAGED(INSTALL_EXPERIMENTAL_NATIVES);
index 4e9111c5a5e695ff37e7ac8177d6ed11ab8eb56d..75e866b1b0876937daa3203dcd98772ebb708fcb 100644 (file)
@@ -23,20 +23,26 @@ namespace internal {
  */
 class NativesStore {
  public:
-  ~NativesStore() {}
+  ~NativesStore() {
+    for (int i = 0; i < native_names_.length(); i++) {
+      native_names_[i].Dispose();
+    }
+  }
 
-  int GetBuiltinsCount() { return native_names_.length(); }
+  int GetBuiltinsCount() { return native_ids_.length(); }
   int GetDebuggerCount() { return debugger_count_; }
+
   Vector<const char> GetScriptName(int index) { return native_names_[index]; }
+
   Vector<const char> GetRawScriptSource(int index) {
     return native_source_[index];
   }
 
-  int GetIndex(const char* name) {
-    for (int i = 0; i < native_names_.length(); ++i) {
-      int native_name_length = native_names_[i].length();
-      if ((static_cast<int>(strlen(name)) == native_name_length) &&
-          (strncmp(name, native_names_[i].start(), native_name_length) == 0)) {
+  int GetIndex(const char* id) {
+    for (int i = 0; i < native_ids_.length(); ++i) {
+      int native_id_length = native_ids_[i].length();
+      if ((static_cast<int>(strlen(id)) == native_id_length) &&
+          (strncmp(id, native_ids_[i].start(), native_id_length) == 0)) {
         return i;
       }
     }
@@ -75,24 +81,35 @@ class NativesStore {
  private:
   NativesStore() : debugger_count_(0) {}
 
+  Vector<const char> NameFromId(const byte* id, int id_length) {
+    Vector<char> name(Vector<char>::New(id_length + 11));
+    SimpleStringBuilder builder(name.start(), name.length());
+    builder.AddString("native ");
+    builder.AddSubstring(reinterpret_cast<const char*>(id), id_length);
+    builder.AddString(".js");
+    return Vector<const char>::cast(name);
+  }
+
   bool ReadNameAndContentPair(SnapshotByteSource* bytes) {
-    const byte* name;
-    int name_length;
+    const byte* id;
+    int id_length;
     const byte* source;
     int source_length;
-    bool success = bytes->GetBlob(&name, &name_length) &&
+    bool success = bytes->GetBlob(&id, &id_length) &&
                    bytes->GetBlob(&source, &source_length);
     if (success) {
-      Vector<const char> name_vector(
-          reinterpret_cast<const char*>(name), name_length);
+      Vector<const char> id_vector(reinterpret_cast<const char*>(id),
+                                   id_length);
       Vector<const char> source_vector(
           reinterpret_cast<const char*>(source), source_length);
-      native_names_.Add(name_vector);
+      native_ids_.Add(id_vector);
       native_source_.Add(source_vector);
+      native_names_.Add(NameFromId(id, id_length));
     }
     return success;
   }
 
+  List<Vector<const char> > native_ids_;
   List<Vector<const char> > native_names_;
   List<Vector<const char> > native_source_;
   int debugger_count_;