Move configuration of ResourceConstraints to Isolate construction
authorjochen@chromium.org <jochen@chromium.org>
Tue, 16 Sep 2014 09:15:02 +0000 (09:15 +0000)
committerjochen@chromium.org <jochen@chromium.org>
Tue, 16 Sep 2014 09:15:02 +0000 (09:15 +0000)
We can only set resource constraints before the isolate is initialized.
Since in the future, we want to initialize isolates at construction
time, we need to set constraints there.

It's possible to later modify the stack limit, so introduce an
Isolate::SetStackLimit method for that.

The SetResourceConstraints method will be deprecated. Users should pass
ResourceConstraints to Isolate::New, and use Isolate::SetStackLimit to
later modify the stack limit.

BUG=none
R=svenpanne@chromium.org
LOG=y

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

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23964 ce2b1a6d-e550-0410-aec6-3dcde31c8c00

include/v8.h
src/api.cc
src/d8.cc
test/cctest/test-api.cc
test/cctest/test-strings.cc

index 39d01af..e4c7087 100644 (file)
@@ -4101,6 +4101,9 @@ class V8_EXPORT ResourceConstraints {
 
 /**
  * Sets the given ResourceConstraints on the given Isolate.
+ *
+ * Deprecated, will be removed. Pass constraints via Isolate::New or modify
+ * the stack limit via Isolate::SetStackLimit.
  */
 bool V8_EXPORT SetResourceConstraints(Isolate* isolate,
                                       ResourceConstraints* constraints);
@@ -4371,6 +4374,11 @@ class V8_EXPORT Isolate {
      * notified each time code is added, moved or removed.
      */
     JitCodeEventHandler code_event_handler;
+
+    /**
+     * ResourceConstraints to use for the new Isolate.
+     */
+    ResourceConstraints constraints;
   };
 
 
@@ -4817,6 +4825,17 @@ class V8_EXPORT Isolate {
   void SetJitCodeEventHandler(JitCodeEventOptions options,
                               JitCodeEventHandler event_handler);
 
+  /**
+   * Modifies the stack limit for this Isolate.
+   *
+   * \param stack_limit An address beyond which the Vm's stack may not grow.
+   *
+   * \note  If you are using threads then you should hold the V8::Locker lock
+   *     while setting the stack limit and you must set a non-default stack
+   *     limit separately for each thread.
+   */
+  void SetStackLimit(uintptr_t stack_limit);
+
  private:
   template<class K, class V, class Traits> friend class PersistentValueMap;
 
index 94b00ef..3c042d7 100644 (file)
@@ -6664,6 +6664,7 @@ Isolate* Isolate::GetCurrent() {
 
 Isolate* Isolate::New(const Isolate::CreateParams& params) {
   i::Isolate* isolate = new i::Isolate();
+  Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
   if (params.entry_hook) {
     isolate->set_function_entry_hook(params.entry_hook);
   }
@@ -6672,7 +6673,9 @@ Isolate* Isolate::New(const Isolate::CreateParams& params) {
     isolate->logger()->SetCodeEventHandler(kJitCodeEventDefault,
                                            params.code_event_handler);
   }
-  return reinterpret_cast<Isolate*>(isolate);
+  SetResourceConstraints(v8_isolate,
+                         const_cast<ResourceConstraints*>(&params.constraints));
+  return v8_isolate;
 }
 
 
@@ -6889,6 +6892,13 @@ void v8::Isolate::SetJitCodeEventHandler(JitCodeEventOptions options,
 }
 
 
+void v8::Isolate::SetStackLimit(uintptr_t stack_limit) {
+  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
+  CHECK(stack_limit);
+  isolate->stack_guard()->SetStackLimit(stack_limit);
+}
+
+
 String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj)
     : str_(NULL), length_(0) {
   i::Isolate* isolate = i::Isolate::Current();
index f22be5b..a288948 100644 (file)
--- a/src/d8.cc
+++ b/src/d8.cc
@@ -1621,14 +1621,13 @@ int Shell::Main(int argc, char* argv[]) {
 #ifdef ENABLE_VTUNE_JIT_INTERFACE
   vTune::InitializeVtuneForV8(create_params);
 #endif
-  Isolate* isolate = Isolate::New(create_params);
 #ifndef V8_SHARED
-  v8::ResourceConstraints constraints;
-  constraints.ConfigureDefaults(base::SysInfo::AmountOfPhysicalMemory(),
-                                base::SysInfo::AmountOfVirtualMemory(),
-                                base::SysInfo::NumberOfProcessors());
-  v8::SetResourceConstraints(isolate, &constraints);
+  create_params.constraints.ConfigureDefaults(
+      base::SysInfo::AmountOfPhysicalMemory(),
+      base::SysInfo::AmountOfVirtualMemory(),
+      base::SysInfo::NumberOfProcessors());
 #endif
+  Isolate* isolate = Isolate::New(create_params);
   DumbLineEditor dumb_line_editor(isolate);
   {
     Isolate::Scope scope(isolate);
index d7f1520..a6e2290 100644 (file)
@@ -17888,13 +17888,11 @@ static uint32_t* ComputeStackLimit(uint32_t size) {
 static const int stack_breathing_room = 256 * i::KB;
 
 
-TEST(SetResourceConstraints) {
+TEST(SetStackLimit) {
   uint32_t* set_limit = ComputeStackLimit(stack_breathing_room);
 
   // Set stack limit.
-  v8::ResourceConstraints constraints;
-  constraints.set_stack_limit(set_limit);
-  CHECK(v8::SetResourceConstraints(CcTest::isolate(), &constraints));
+  CcTest::isolate()->SetStackLimit(reinterpret_cast<uintptr_t>(set_limit));
 
   // Execute a script.
   LocalContext env;
@@ -17909,16 +17907,14 @@ TEST(SetResourceConstraints) {
 }
 
 
-TEST(SetResourceConstraintsInThread) {
+TEST(SetStackLimitInThread) {
   uint32_t* set_limit;
   {
     v8::Locker locker(CcTest::isolate());
     set_limit = ComputeStackLimit(stack_breathing_room);
 
     // Set stack limit.
-    v8::ResourceConstraints constraints;
-    constraints.set_stack_limit(set_limit);
-    CHECK(v8::SetResourceConstraints(CcTest::isolate(), &constraints));
+    CcTest::isolate()->SetStackLimit(reinterpret_cast<uintptr_t>(set_limit));
 
     // Execute a script.
     v8::HandleScope scope(CcTest::isolate());
@@ -19579,16 +19575,22 @@ class InitDefaultIsolateThread : public v8::base::Thread {
         result_(false) {}
 
   void Run() {
-    v8::Isolate* isolate = v8::Isolate::New();
-    isolate->Enter();
+    v8::Isolate::CreateParams create_params;
     switch (testCase_) {
       case SetResourceConstraints: {
-        v8::ResourceConstraints constraints;
-        constraints.set_max_semi_space_size(1);
-        constraints.set_max_old_space_size(4);
-        v8::SetResourceConstraints(CcTest::isolate(), &constraints);
+        create_params.constraints.set_max_semi_space_size(1);
+        create_params.constraints.set_max_old_space_size(4);
         break;
       }
+      default:
+        break;
+    }
+    v8::Isolate* isolate = v8::Isolate::New(create_params);
+    isolate->Enter();
+    switch (testCase_) {
+      case SetResourceConstraints:
+        // Already handled in pre-Isolate-creation block.
+        break;
 
       case SetFatalHandler:
         v8::V8::SetFatalErrorHandler(NULL);
index d384e30..ef13c4d 100644 (file)
@@ -1209,28 +1209,34 @@ TEST(SliceFromSlice) {
 }
 
 
-TEST(OneByteArrayJoin) {
+UNINITIALIZED_TEST(OneByteArrayJoin) {
+  v8::Isolate::CreateParams create_params;
   // Set heap limits.
-  v8::ResourceConstraints constraints;
-  constraints.set_max_semi_space_size(1);
-  constraints.set_max_old_space_size(4);
-  v8::SetResourceConstraints(CcTest::isolate(), &constraints);
-
-  // String s is made of 2^17 = 131072 'c' characters and a is an array
-  // starting with 'bad', followed by 2^14 times the string s. That means the
-  // total length of the concatenated strings is 2^31 + 3. So on 32bit systems
-  // summing the lengths of the strings (as Smis) overflows and wraps.
-  LocalContext context;
-  v8::HandleScope scope(CcTest::isolate());
-  v8::TryCatch try_catch;
-  CHECK(CompileRun(
-      "var two_14 = Math.pow(2, 14);"
-      "var two_17 = Math.pow(2, 17);"
-      "var s = Array(two_17 + 1).join('c');"
-      "var a = ['bad'];"
-      "for (var i = 1; i <= two_14; i++) a.push(s);"
-      "a.join("");").IsEmpty());
-  CHECK(try_catch.HasCaught());
+  create_params.constraints.set_max_semi_space_size(1);
+  create_params.constraints.set_max_old_space_size(4);
+  v8::Isolate* isolate = v8::Isolate::New(create_params);
+  isolate->Enter();
+
+  {
+    // String s is made of 2^17 = 131072 'c' characters and a is an array
+    // starting with 'bad', followed by 2^14 times the string s. That means the
+    // total length of the concatenated strings is 2^31 + 3. So on 32bit systems
+    // summing the lengths of the strings (as Smis) overflows and wraps.
+    LocalContext context(isolate);
+    v8::HandleScope scope(isolate);
+    v8::TryCatch try_catch;
+    CHECK(CompileRun(
+              "var two_14 = Math.pow(2, 14);"
+              "var two_17 = Math.pow(2, 17);"
+              "var s = Array(two_17 + 1).join('c');"
+              "var a = ['bad'];"
+              "for (var i = 1; i <= two_14; i++) a.push(s);"
+              "a.join("
+              ");").IsEmpty());
+    CHECK(try_catch.HasCaught());
+  }
+  isolate->Exit();
+  isolate->Dispose();
 }