From c3dfdbe772f9acde6f5fbb74aa46ba251a81ecda Mon Sep 17 00:00:00 2001 From: "ajohnsen@google.com" Date: Thu, 6 Mar 2014 09:16:41 +0000 Subject: [PATCH] Fix fromEnvironment when called from isolates. BUG= R=sgjesse@google.com Review URL: https://codereview.chromium.org//180243022 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@33363 260f80e4-7a28-3924-810f-c04153c831b5 --- runtime/lib/bool.cc | 1 + runtime/lib/integers.cc | 1 + runtime/lib/string.cc | 1 + runtime/vm/dart_api_impl.h | 14 ++++++++++++++ runtime/vm/parser.cc | 16 +--------------- ...bool_from_environment_default_value_test.dart | 12 +++++++++++- .../int_from_environment_default_value_test.dart | 12 +++++++++++- .../string_from_environment_default_value.dart | 12 +++++++++++- 8 files changed, 51 insertions(+), 18 deletions(-) diff --git a/runtime/lib/bool.cc b/runtime/lib/bool.cc index d57574568dd..c78ea89ecaa 100644 --- a/runtime/lib/bool.cc +++ b/runtime/lib/bool.cc @@ -21,6 +21,7 @@ DEFINE_NATIVE_ENTRY(Bool_fromEnvironment, 3) { GET_NON_NULL_NATIVE_ARGUMENT(String, name, arguments->NativeArgAt(1)); GET_NATIVE_ARGUMENT(Bool, default_value, arguments->NativeArgAt(2)); // Call the embedder to supply us with the environment. + Api::Scope api_scope(isolate); Dart_EnvironmentCallback callback = isolate->environment_callback(); if (callback != NULL) { Dart_Handle result = callback(Api::NewHandle(isolate, name.raw())); diff --git a/runtime/lib/integers.cc b/runtime/lib/integers.cc index 4e64ea5ae04..a0397d51379 100644 --- a/runtime/lib/integers.cc +++ b/runtime/lib/integers.cc @@ -239,6 +239,7 @@ DEFINE_NATIVE_ENTRY(Integer_fromEnvironment, 3) { GET_NON_NULL_NATIVE_ARGUMENT(String, name, arguments->NativeArgAt(1)); GET_NATIVE_ARGUMENT(Integer, default_value, arguments->NativeArgAt(2)); // Call the embedder to supply us with the environment. + Api::Scope api_scope(isolate); Dart_EnvironmentCallback callback = isolate->environment_callback(); if (callback != NULL) { Dart_Handle response = callback(Api::NewHandle(isolate, name.raw())); diff --git a/runtime/lib/string.cc b/runtime/lib/string.cc index 47a82ed6069..1a2e60b1975 100644 --- a/runtime/lib/string.cc +++ b/runtime/lib/string.cc @@ -19,6 +19,7 @@ DEFINE_NATIVE_ENTRY(String_fromEnvironment, 3) { GET_NON_NULL_NATIVE_ARGUMENT(String, name, arguments->NativeArgAt(1)); GET_NATIVE_ARGUMENT(String, default_value, arguments->NativeArgAt(2)); // Call the embedder to supply us with the environment. + Api::Scope api_scope(); Dart_EnvironmentCallback callback = isolate->environment_callback(); if (callback != NULL) { Dart_Handle result = callback(Api::NewHandle(isolate, name.raw())); diff --git a/runtime/vm/dart_api_impl.h b/runtime/vm/dart_api_impl.h index 2f10f43388d..3a5cf64db62 100644 --- a/runtime/vm/dart_api_impl.h +++ b/runtime/vm/dart_api_impl.h @@ -109,6 +109,20 @@ const char* CanonicalFunction(const char* func); class Api : AllStatic { public: + // Create on the stack to provide a new throw-safe api scope. + class Scope : public StackResource { + public: + explicit Scope(Isolate* isolate) : StackResource(isolate) { + Dart_EnterScope(); + } + ~Scope() { + Dart_ExitScope(); + } + + private: + DISALLOW_COPY_AND_ASSIGN(Scope); + }; + // Creates a new local handle. static Dart_Handle NewHandle(Isolate* isolate, RawObject* raw); diff --git a/runtime/vm/parser.cc b/runtime/vm/parser.cc index 89164917014..7477af6bff1 100644 --- a/runtime/vm/parser.cc +++ b/runtime/vm/parser.cc @@ -4855,20 +4855,6 @@ void Parser::ParseTopLevelAccessor(TopLevel* top_level, } -class DartApiScope : public StackResource { - public: - explicit DartApiScope(Isolate* isolate) : StackResource(isolate) { - Dart_EnterScope(); - } - ~DartApiScope() { - Dart_ExitScope(); - } - - private: - DISALLOW_COPY_AND_ASSIGN(DartApiScope); -}; - - RawObject* Parser::CallLibraryTagHandler(Dart_LibraryTag tag, intptr_t token_pos, const String& url) { @@ -4885,7 +4871,7 @@ RawObject* Parser::CallLibraryTagHandler(Dart_LibraryTag tag, // Block class finalization attempts when calling into the library // tag handler. isolate()->BlockClassFinalization(); - DartApiScope api_scope(isolate()); + Api::Scope api_scope(isolate()); Dart_Handle result = handler(tag, Api::NewHandle(isolate(), library_.raw()), Api::NewHandle(isolate(), url.raw())); diff --git a/tests/corelib/bool_from_environment_default_value_test.dart b/tests/corelib/bool_from_environment_default_value_test.dart index f41252238f3..1095e0e0f21 100644 --- a/tests/corelib/bool_from_environment_default_value_test.dart +++ b/tests/corelib/bool_from_environment_default_value_test.dart @@ -2,11 +2,21 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import "dart:isolate"; + import "package:expect/expect.dart"; -main() { +void test(port) { Expect.isFalse(const bool.fromEnvironment('NOT_FOUND')); Expect.isTrue(const bool.fromEnvironment('NOT_FOUND', defaultValue: true)); Expect.isFalse(const bool.fromEnvironment('NOT_FOUND', defaultValue: false)); Expect.isNull(const bool.fromEnvironment('NOT_FOUND', defaultValue: null)); + if (port != null) port.send(null); +} + +main() { + test(null); + var port = new ReceivePort(); + Isolate.spawn(test, port.sendPort); + port.listen((_) => port.close()); } diff --git a/tests/corelib/int_from_environment_default_value_test.dart b/tests/corelib/int_from_environment_default_value_test.dart index d97145a6e6e..4a04e74ffa7 100644 --- a/tests/corelib/int_from_environment_default_value_test.dart +++ b/tests/corelib/int_from_environment_default_value_test.dart @@ -2,10 +2,20 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import "dart:isolate"; + import "package:expect/expect.dart"; -main() { +void test(port) { Expect.isNull(const int.fromEnvironment('NOT_FOUND')); Expect.equals(12345, const int.fromEnvironment('NOT_FOUND', defaultValue: 12345)); + if (port != null) port.send(null); +} + +main() { + test(null); + var port = new ReceivePort(); + Isolate.spawn(test, port.sendPort); + port.listen((_) => port.close()); } diff --git a/tests/corelib/string_from_environment_default_value.dart b/tests/corelib/string_from_environment_default_value.dart index 3d236e7ea4e..b361dd30297 100644 --- a/tests/corelib/string_from_environment_default_value.dart +++ b/tests/corelib/string_from_environment_default_value.dart @@ -2,10 +2,20 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import "dart:isolate"; + import "package:expect/expect.dart"; -main() { +void test(port) { Expect.isNull(const String.fromEnvironment('NOT_FOUND')); Expect.equals('x', const String.fromEnvironment('NOT_FOUND', defaultValue: 'x')); + if (port != null) port.send(null); +} + +main() { + test(null); + var port = new ReceivePort(); + Isolate.spawn(test, port.sendPort); + port.listen((_) => port.close()); }