Skip to content

Commit ea837c9

Browse files
aamcommit-bot@chromium.org
authored andcommitted
Transition tests to EvaluateCompiledExpression.
This is follow-up to https://dart-review.googlesource.com/c/sdk/+/58660. Change-Id: Ic0ae58eb34dabe6e0902f37c8e817f949ed4ac64 Reviewed-on: https://dart-review.googlesource.com/60705 Commit-Queue: Alexander Aprelev <aam@google.com> Reviewed-by: Ryan Macnak <rmacnak@google.com>
1 parent 3e6e2dd commit ea837c9

File tree

8 files changed

+97
-44
lines changed

8 files changed

+97
-44
lines changed

runtime/observatory/tests/service/service_kernel.status

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,6 @@ code_test: RuntimeError
149149
step_test: RuntimeError
150150

151151
[ $compiler == dartk && $mode == debug ]
152-
eval_internal_class_test: RuntimeError
153-
eval_test: RuntimeError
154152
isolate_lifecycle_test: Skip # Flaky.
155153
pause_idle_isolate_test: Skip # Flaky
156154

runtime/vm/compiler_test.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,13 @@ ISOLATE_UNIT_TEST_CASE(EvalExpressionWithLazyCompile) {
208208
TestCase::LoadTestScript("", NULL);
209209
}
210210
Library& lib = Library::Handle(Library::CoreLibrary());
211-
212211
const String& expression = String::Handle(
213212
String::New("(){ return (){ return (){ return 3 + 4; }(); }(); }()"));
214213
Object& val = Object::Handle();
215-
val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array());
214+
val = Api::UnwrapHandle(
215+
TestCase::EvaluateExpression(lib, expression,
216+
/* param_names= */ Array::empty_array(),
217+
/* param_values= */ Array::empty_array()));
216218

217219
EXPECT(!val.IsNull());
218220
EXPECT(!val.IsError());
@@ -226,12 +228,13 @@ ISOLATE_UNIT_TEST_CASE(EvalExpressionExhaustCIDs) {
226228
TestCase::LoadTestScript("", NULL);
227229
}
228230
Library& lib = Library::Handle(Library::CoreLibrary());
229-
230231
const String& expression = String::Handle(String::New("3 + 4"));
231232
Object& val = Object::Handle();
233+
val = Api::UnwrapHandle(
234+
TestCase::EvaluateExpression(lib, expression,
235+
/* param_names= */ Array::empty_array(),
236+
/* param_values= */ Array::empty_array()));
232237

233-
// Run once to ensure everything we touch is compiled.
234-
val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array());
235238
EXPECT(!val.IsNull());
236239
EXPECT(!val.IsError());
237240
EXPECT(val.IsInteger());
@@ -240,7 +243,10 @@ ISOLATE_UNIT_TEST_CASE(EvalExpressionExhaustCIDs) {
240243
intptr_t initial_class_table_size =
241244
Isolate::Current()->class_table()->NumCids();
242245

243-
val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array());
246+
val = Api::UnwrapHandle(
247+
TestCase::EvaluateExpression(lib, expression,
248+
/* param_names= */ Array::empty_array(),
249+
/* param_values= */ Array::empty_array()));
244250
EXPECT(!val.IsNull());
245251
EXPECT(!val.IsError());
246252
EXPECT(val.IsInteger());

runtime/vm/debugger_api_impl_test.cc

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
#include <include/dart_api.h>
56
#include "include/dart_tools_api.h"
67

78
#include "vm/class_finalizer.h"
@@ -232,39 +233,50 @@ DART_EXPORT Dart_Handle Dart_SetBreakpoint(Dart_Handle script_url_in,
232233
return Dart_NewInteger(bpt->id());
233234
}
234235

235-
DART_EXPORT Dart_Handle Dart_EvaluateExpr(Dart_Handle target_in,
236-
Dart_Handle expr_in) {
236+
DART_EXPORT Dart_Handle Dart_EvaluateStaticExpr(Dart_Handle lib_handle,
237+
Dart_Handle expr_in) {
237238
DARTSCOPE(Thread::Current());
238239
CHECK_DEBUGGER(T->isolate());
239240

240-
const Object& target = Object::Handle(Z, Api::UnwrapHandle(target_in));
241-
if (target.IsError()) return target_in;
241+
const Object& target = Object::Handle(Z, Api::UnwrapHandle(lib_handle));
242+
if (target.IsError()) return lib_handle;
242243
if (target.IsNull()) {
243244
return Api::NewError("%s expects argument 'target' to be non-null",
244245
CURRENT_FUNC);
245246
}
247+
const Library& lib = Library::Cast(target);
246248
UNWRAP_AND_CHECK_PARAM(String, expr, expr_in);
247-
// Type extends Instance, must check first.
248-
if (target.IsType()) {
249-
const Class& cls = Class::Handle(Z, Type::Cast(target).type_class());
250-
return Api::NewHandle(
251-
T, cls.Evaluate(expr, Array::empty_array(), Array::empty_array()));
252-
} else if (target.IsInstance()) {
253-
const Instance& inst = Instance::Cast(target);
254-
const Class& receiver_cls = Class::Handle(Z, inst.clazz());
255-
return Api::NewHandle(
256-
T, inst.Evaluate(receiver_cls, expr, Array::empty_array(),
257-
Array::empty_array()));
258-
} else if (target.IsLibrary()) {
259-
const Library& lib = Library::Cast(target);
249+
250+
if (!KernelIsolate::IsRunning()) {
260251
return Api::NewHandle(
261252
T, lib.Evaluate(expr, Array::empty_array(), Array::empty_array()));
262-
} else if (target.IsClass()) {
263-
const Class& cls = Class::Cast(target);
264-
return Api::NewHandle(
265-
T, cls.Evaluate(expr, Array::empty_array(), Array::empty_array()));
253+
} else {
254+
Dart_KernelCompilationResult compilation_result;
255+
{
256+
TransitionVMToNative transition(T);
257+
compilation_result = KernelIsolate::CompileExpressionToKernel(
258+
expr.ToCString(),
259+
/* definitions= */ Array::empty_array(),
260+
/* type_defintions= */ Array::empty_array(),
261+
String::Handle(lib.url()).ToCString(),
262+
/* klass= */ nullptr,
263+
/* is_static= */ false);
264+
}
265+
if (compilation_result.status != Dart_KernelCompilationStatus_Ok) {
266+
return Api::NewError("Failed to compile expression.");
267+
}
268+
269+
const uint8_t* kernel_bytes = compilation_result.kernel;
270+
intptr_t kernel_length = compilation_result.kernel_size;
271+
return Api::NewHandle(T, lib.EvaluateCompiledExpression(
272+
kernel_bytes, kernel_length,
273+
/* type_definitions= */
274+
Array::empty_array(),
275+
/* param_values= */
276+
Array::empty_array(),
277+
/* type_param_values= */
278+
TypeArguments::null_type_arguments()));
266279
}
267-
return Api::NewError("%s: unsupported target type", CURRENT_FUNC);
268280
}
269281

270282
DART_EXPORT Dart_Handle Dart_GetLibraryIds() {

runtime/vm/debugger_api_impl_test.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,23 +180,16 @@ Dart_ActivationFrameInfo(Dart_ActivationFrame activation_frame,
180180

181181
/**
182182
* Execute the expression given in string \expr in the context
183-
* of \target.
183+
* of \lib_handle library, as if it were a top-level function in it.
184184
*
185185
* Requires there to be a current isolate.
186186
*
187-
* The expression is evaluated in the context of \target.
188-
* If \target is a Dart object, the expression is evaluated as if
189-
* it were an instance method of the class of the object.
190-
* If \target is a Class, the expression is evaluated as if it
191-
* were a static method of that class.
192-
* If \target is a Library, the expression is evaluated as if it
193-
* were a top-level function in that library.
194-
*
195187
* \return A handle to the computed value, or an error object if
196188
* the compilation of the expression fails, or if the evaluation throws
197189
* an error.
198190
*/
199-
DART_EXPORT Dart_Handle Dart_EvaluateExpr(Dart_Handle target, Dart_Handle expr);
191+
DART_EXPORT Dart_Handle Dart_EvaluateStaticExpr(Dart_Handle lib_handle,
192+
Dart_Handle expr);
200193

201194
/**
202195
* Returns a handle to the library \library_id.

runtime/vm/object_test.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
#include "include/dart_api.h"
6+
57
#include "platform/globals.h"
68

79
#include "vm/class_finalizer.h"
@@ -4356,7 +4358,9 @@ TEST_CASE(HashCode) {
43564358
EXPECT(result.IsIdenticalTo(expected));
43574359
}
43584360

4359-
static void CheckIdenticalHashStructure(const Instance& a, const Instance& b) {
4361+
static void CheckIdenticalHashStructure(Thread* T,
4362+
const Instance& a,
4363+
const Instance& b) {
43604364
const char* kScript =
43614365
"(a, b) {\n"
43624366
" if (a._usedData != b._usedData ||\n"
@@ -4391,7 +4395,8 @@ static void CheckIdenticalHashStructure(const Instance& a, const Instance& b) {
43914395
param_values.SetAt(1, b);
43924396
name = String::New(kScript);
43934397
Library& lib = Library::Handle(Library::CollectionLibrary());
4394-
EXPECT(lib.Evaluate(name, param_names, param_values) == Bool::True().raw());
4398+
EXPECT(Api::UnwrapHandle(TestCase::EvaluateExpression(
4399+
lib, name, param_names, param_values)) == Bool::True().raw());
43954400
}
43964401

43974402
TEST_CASE(LinkedHashMap) {
@@ -4419,7 +4424,7 @@ TEST_CASE(LinkedHashMap) {
44194424

44204425
// 3. Expect them to have identical structure.
44214426
TransitionNativeToVM transition(thread);
4422-
CheckIdenticalHashStructure(dart_map, cc_map);
4427+
CheckIdenticalHashStructure(thread, dart_map, cc_map);
44234428
}
44244429

44254430
TEST_CASE(LinkedHashMap_iteration) {

runtime/vm/service_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ServiceTestMessageHandler : public MessageHandler {
7676

7777
static RawArray* Eval(Dart_Handle lib, const char* expr) {
7878
const String& dummy_isolate_id = String::Handle(String::New("isolateId"));
79-
Dart_Handle expr_val = Dart_EvaluateExpr(lib, NewString(expr));
79+
Dart_Handle expr_val = Dart_EvaluateStaticExpr(lib, NewString(expr));
8080
EXPECT_VALID(expr_val);
8181
Zone* zone = Thread::Current()->zone();
8282
const GrowableObjectArray& value =

runtime/vm/unit_test.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,40 @@ Dart_Handle TestCase::library_handler(Dart_LibraryTag tag,
689689
return Api::Success();
690690
}
691691

692+
Dart_Handle TestCase::EvaluateExpression(const Library& lib,
693+
const String& expr,
694+
const Array& param_names,
695+
const Array& param_values) {
696+
Thread* thread = Thread::Current();
697+
698+
Object& val = Object::Handle();
699+
if (!KernelIsolate::IsRunning()) {
700+
val = lib.Evaluate(expr, param_names, param_values);
701+
} else {
702+
Dart_KernelCompilationResult compilation_result;
703+
{
704+
TransitionVMToNative transition(thread);
705+
compilation_result = KernelIsolate::CompileExpressionToKernel(
706+
expr.ToCString(), param_names, Array::empty_array(),
707+
String::Handle(lib.url()).ToCString(), /* klass=*/nullptr,
708+
/* is_static= */ false);
709+
}
710+
if (compilation_result.status != Dart_KernelCompilationStatus_Ok) {
711+
return Dart_NewApiError(compilation_result.error);
712+
}
713+
714+
const uint8_t* kernel_bytes = compilation_result.kernel;
715+
intptr_t kernel_length = compilation_result.kernel_size;
716+
717+
val = lib.EvaluateCompiledExpression(kernel_bytes, kernel_length,
718+
Array::empty_array(), param_values,
719+
TypeArguments::null_type_arguments());
720+
721+
free(const_cast<uint8_t*>(kernel_bytes));
722+
}
723+
return Api::NewHandle(thread, val.raw());
724+
}
725+
692726
#if !defined(PRODUCT)
693727
static bool IsHex(int c) {
694728
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f');

runtime/vm/unit_test.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ class TestCase : TestCaseBase {
350350
static Dart_Handle LoadCoreTestScript(const char* script,
351351
Dart_NativeEntryResolver resolver);
352352

353+
static Dart_Handle EvaluateExpression(const Library& lib,
354+
const String& expr,
355+
const Array& param_names,
356+
const Array& param_values);
357+
353358
static Dart_Handle lib();
354359
static const char* url();
355360
static Dart_Isolate CreateTestIsolateFromSnapshot(uint8_t* buffer,

0 commit comments

Comments
 (0)