Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(330)

Side by Side Diff: vm/dart_api_impl_test.cc

Issue 10535180: Allow implicit 'close your eyes' of native methods. This will hopefully fix issue 3466. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/runtime/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « vm/code_patcher_x64_test.cc ('k') | vm/flow_graph_compiler_ia32.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "include/dart_api.h" 5 #include "include/dart_api.h"
6 #include "platform/assert.h" 6 #include "platform/assert.h"
7 #include "platform/utils.h" 7 #include "platform/utils.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/dart_api_impl.h" 9 #include "vm/dart_api_impl.h"
10 #include "vm/dart_api_state.h" 10 #include "vm/dart_api_state.h"
(...skipping 5154 matching lines...) Expand 10 before | Expand all | Expand 10 after
5165 } 5165 }
5166 } 5166 }
5167 5167
5168 // We should have received the expected number of interrupts. 5168 // We should have received the expected number of interrupts.
5169 EXPECT_EQ(kInterruptCount, interrupt_count); 5169 EXPECT_EQ(kInterruptCount, interrupt_count);
5170 5170
5171 // Give the spawned thread enough time to properly exit. 5171 // Give the spawned thread enough time to properly exit.
5172 Isolate::SetInterruptCallback(saved); 5172 Isolate::SetInterruptCallback(saved);
5173 } 5173 }
5174 5174
5175
5176 static int64_t GetValue(Dart_Handle arg) {
5177 EXPECT(!Dart_IsError(arg));
5178 EXPECT(Dart_IsInteger(arg));
5179 int64_t value;
5180 EXPECT_VALID(Dart_IntegerToInt64(arg, &value));
5181 return value;
5182 }
5183
5184 static void NativeFoo1(Dart_NativeArguments args) {
5185 Dart_EnterScope();
5186 intptr_t i = Dart_GetNativeArgumentCount(args);
5187 EXPECT_EQ(1, i);
5188 Dart_Handle arg = Dart_GetNativeArgument(args, 0);
5189 EXPECT(!Dart_IsError(arg));
5190 Dart_SetReturnValue(args, Dart_NewInteger(1));
5191 Dart_ExitScope();
5192 }
5193
5194
5195 static void NativeFoo2(Dart_NativeArguments args) {
5196 Dart_EnterScope();
5197 intptr_t i = Dart_GetNativeArgumentCount(args);
5198 EXPECT_EQ(2, i);
5199 Dart_Handle arg = Dart_GetNativeArgument(args, 1);
5200 Dart_SetReturnValue(args, Dart_NewInteger(GetValue(arg)));
5201 Dart_ExitScope();
5202 }
5203
5204
5205 static void NativeFoo3(Dart_NativeArguments args) {
5206 Dart_EnterScope();
5207 intptr_t i = Dart_GetNativeArgumentCount(args);
5208 EXPECT_EQ(3, i);
5209 Dart_Handle arg1 = Dart_GetNativeArgument(args, 1);
5210 Dart_Handle arg2 = Dart_GetNativeArgument(args, 2);
5211 Dart_SetReturnValue(args, Dart_NewInteger(GetValue(arg1) + GetValue(arg2)));
5212 Dart_ExitScope();
5213 }
5214
5215
5216 static void NativeFoo4(Dart_NativeArguments args) {
5217 Dart_EnterScope();
5218 intptr_t i = Dart_GetNativeArgumentCount(args);
5219 EXPECT_EQ(4, i);
5220 Dart_Handle arg1 = Dart_GetNativeArgument(args, 1);
5221 Dart_Handle arg2 = Dart_GetNativeArgument(args, 2);
5222 Dart_Handle arg3 = Dart_GetNativeArgument(args, 3);
5223 Dart_SetReturnValue(args, Dart_NewInteger(GetValue(arg1) +
5224 GetValue(arg2) +
5225 GetValue(arg3)));
5226 Dart_ExitScope();
5227 }
5228
5229
5230 static Dart_NativeFunction MyNativeClosureResolver(Dart_Handle name,
5231 int arg_count) {
5232 const Object& obj = Object::Handle(Api::UnwrapHandle(name));
5233 if (!obj.IsString()) {
5234 return NULL;
5235 }
5236 const char* function_name = obj.ToCString();
5237 const char* kNativeFoo1 = "NativeFoo1";
5238 const char* kNativeFoo2 = "NativeFoo2";
5239 const char* kNativeFoo3 = "NativeFoo3";
5240 const char* kNativeFoo4 = "NativeFoo4";
5241 if (!strncmp(function_name, kNativeFoo1, strlen(kNativeFoo1))) {
5242 return &NativeFoo1;
5243 } else if (!strncmp(function_name, kNativeFoo2, strlen(kNativeFoo2))) {
5244 return &NativeFoo2;
5245 } else if (!strncmp(function_name, kNativeFoo3, strlen(kNativeFoo3))) {
5246 return &NativeFoo3;
5247 } else if (!strncmp(function_name, kNativeFoo4, strlen(kNativeFoo4))) {
5248 return &NativeFoo4;
5249 } else {
5250 UNREACHABLE();
5251 return NULL;
5252 }
5253 }
5254
5255
5256 TEST_CASE(NativeFunctionClosure) {
5257 const char* kScriptChars =
5258 "class Test {"
5259 " int foo1() native \"NativeFoo1\";\n"
5260 " int foo2(int i) native \"NativeFoo2\";\n"
5261 " int foo3([int k = 10000, int l = 1]) native \"NativeFoo3\";\n"
5262 " int foo4(int i,"
5263 " [int j = 10, int k = 1]) native \"NativeFoo4\";\n"
5264 " int bar1() { var func = foo1; return func(); }\n"
5265 " int bar2(int i) { var func = foo2; return func(i); }\n"
5266 " int bar30() { var func = foo3; return func(); }\n"
5267 " int bar31(int i) { var func = foo3; return func(i); }\n"
5268 " int bar32(int i, int j) { var func = foo3; return func(i, j); }\n"
5269 " int bar41(int i) {\n"
5270 " var func = foo4; return func(i); }\n"
5271 " int bar42(int i, int j) {\n"
5272 " var func = foo4; return func(i, j); }\n"
5273 " int bar43(int i, int j, int k) {\n"
5274 " var func = foo4; return func(i, j, k); }\n"
5275 "}\n"
5276 "int testMain() {\n"
5277 " Test obj = new Test();\n"
5278 " Expect.equals(1, obj.foo1());\n"
5279 " Expect.equals(1, obj.bar1());\n"
5280 "\n"
5281 " Expect.equals(10, obj.foo2(10));\n"
5282 " Expect.equals(10, obj.bar2(10));\n"
5283 "\n"
5284 " Expect.equals(10001, obj.foo3());\n"
5285 " Expect.equals(10001, obj.bar30());\n"
5286 " Expect.equals(2, obj.foo3(1));\n"
5287 " Expect.equals(2, obj.bar31(1));\n"
5288 " Expect.equals(4, obj.foo3(2, 2));\n"
5289 " Expect.equals(4, obj.bar32(2, 2));\n"
5290 "\n"
5291 " Expect.equals(12, obj.foo4(1));\n"
5292 " Expect.equals(12, obj.bar41(1));\n"
5293 " Expect.equals(3, obj.foo4(1, 1));\n"
5294 " Expect.equals(3, obj.bar42(1, 1));\n"
5295 " Expect.equals(6, obj.foo4(2, 2, 2));\n"
5296 " Expect.equals(6, obj.bar43(2, 2, 2));\n"
5297 "\n"
5298 " return 0;\n"
5299 "}\n";
5300
5301 Dart_Handle result;
5302
5303 // Load a test script.
5304 Dart_Handle url = Dart_NewString(TestCase::url());
5305 Dart_Handle source = Dart_NewString(kScriptChars);
5306 result = Dart_SetLibraryTagHandler(library_handler);
5307 EXPECT_VALID(result);
5308 Dart_Handle lib = Dart_LoadScript(url, source);
5309 EXPECT_VALID(lib);
5310 EXPECT(Dart_IsLibrary(lib));
5311 result = Dart_SetNativeResolver(lib, &MyNativeClosureResolver);
5312 EXPECT_VALID(result);
5313
5314 result = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
5315 EXPECT_VALID(result);
5316 EXPECT(Dart_IsInteger(result));
5317 int64_t value = 0;
5318 EXPECT_VALID(Dart_IntegerToInt64(result, &value));
5319 EXPECT_EQ(0, value);
5320 }
5321
5322
5323 static void StaticNativeFoo1(Dart_NativeArguments args) {
5324 Dart_EnterScope();
5325 intptr_t i = Dart_GetNativeArgumentCount(args);
5326 EXPECT_EQ(0, i);
5327 Dart_SetReturnValue(args, Dart_NewInteger(0));
5328 Dart_ExitScope();
5329 }
5330
5331
5332 static void StaticNativeFoo2(Dart_NativeArguments args) {
5333 Dart_EnterScope();
5334 intptr_t i = Dart_GetNativeArgumentCount(args);
5335 EXPECT_EQ(1, i);
5336 Dart_Handle arg = Dart_GetNativeArgument(args, 0);
5337 Dart_SetReturnValue(args, Dart_NewInteger(GetValue(arg)));
5338 Dart_ExitScope();
5339 }
5340
5341
5342 static void StaticNativeFoo3(Dart_NativeArguments args) {
5343 Dart_EnterScope();
5344 intptr_t i = Dart_GetNativeArgumentCount(args);
5345 EXPECT_EQ(2, i);
5346 Dart_Handle arg1 = Dart_GetNativeArgument(args, 0);
5347 Dart_Handle arg2 = Dart_GetNativeArgument(args, 1);
5348 Dart_SetReturnValue(args, Dart_NewInteger(GetValue(arg1) + GetValue(arg2)));
5349 Dart_ExitScope();
5350 }
5351
5352
5353 static void StaticNativeFoo4(Dart_NativeArguments args) {
5354 Dart_EnterScope();
5355 intptr_t i = Dart_GetNativeArgumentCount(args);
5356 EXPECT_EQ(3, i);
5357 Dart_Handle arg1 = Dart_GetNativeArgument(args, 0);
5358 Dart_Handle arg2 = Dart_GetNativeArgument(args, 1);
5359 Dart_Handle arg3 = Dart_GetNativeArgument(args, 2);
5360 Dart_SetReturnValue(args, Dart_NewInteger(GetValue(arg1) +
5361 GetValue(arg2) +
5362 GetValue(arg3)));
5363 Dart_ExitScope();
5364 }
5365
5366
5367 static Dart_NativeFunction MyStaticNativeClosureResolver(Dart_Handle name,
5368 int arg_count) {
5369 const Object& obj = Object::Handle(Api::UnwrapHandle(name));
5370 if (!obj.IsString()) {
5371 return NULL;
5372 }
5373 const char* function_name = obj.ToCString();
5374 const char* kNativeFoo1 = "StaticNativeFoo1";
5375 const char* kNativeFoo2 = "StaticNativeFoo2";
5376 const char* kNativeFoo3 = "StaticNativeFoo3";
5377 const char* kNativeFoo4 = "StaticNativeFoo4";
5378 if (!strncmp(function_name, kNativeFoo1, strlen(kNativeFoo1))) {
5379 return &StaticNativeFoo1;
5380 } else if (!strncmp(function_name, kNativeFoo2, strlen(kNativeFoo2))) {
5381 return &StaticNativeFoo2;
5382 } else if (!strncmp(function_name, kNativeFoo3, strlen(kNativeFoo3))) {
5383 return &StaticNativeFoo3;
5384 } else if (!strncmp(function_name, kNativeFoo4, strlen(kNativeFoo4))) {
5385 return &StaticNativeFoo4;
5386 } else {
5387 UNREACHABLE();
5388 return NULL;
5389 }
5390 }
5391
5392
5393 TEST_CASE(NativeStaticFunctionClosure) {
5394 const char* kScriptChars =
5395 "class Test {"
5396 " static int foo1() native \"StaticNativeFoo1\";\n"
5397 " static int foo2(int i) native \"StaticNativeFoo2\";\n"
5398 " static int foo3([int k = 10000, int l = 1])"
5399 " native \"StaticNativeFoo3\";\n"
5400 " static int foo4(int i, [int j = 10, int k = 1])"
5401 " native \"StaticNativeFoo4\";\n"
5402 " int bar1() { var func = foo1; return func(); }\n"
5403 " int bar2(int i) { var func = foo2; return func(i); }\n"
5404 " int bar30() { var func = foo3; return func(); }\n"
5405 " int bar31(int i) { var func = foo3; return func(i); }\n"
5406 " int bar32(int i, int j) { var func = foo3; return func(i, j); }\n"
5407 " int bar41(int i) {\n"
5408 " var func = foo4; return func(i); }\n"
5409 " int bar42(int i, int j) {\n"
5410 " var func = foo4; return func(i, j); }\n"
5411 " int bar43(int i, int j, int k) {\n"
5412 " var func = foo4; return func(i, j, k); }\n"
5413 "}\n"
5414 "int testMain() {\n"
5415 " Test obj = new Test();\n"
5416 " Expect.equals(0, Test.foo1());\n"
5417 " Expect.equals(0, obj.bar1());\n"
5418 "\n"
5419 " Expect.equals(10, Test.foo2(10));\n"
5420 " Expect.equals(10, obj.bar2(10));\n"
5421 "\n"
5422 " Expect.equals(10001, Test.foo3());\n"
5423 " Expect.equals(10001, obj.bar30());\n"
5424 " Expect.equals(2, Test.foo3(1));\n"
5425 " Expect.equals(2, obj.bar31(1));\n"
5426 " Expect.equals(4, Test.foo3(2, 2));\n"
5427 " Expect.equals(4, obj.bar32(2, 2));\n"
5428 "\n"
5429 " Expect.equals(12, Test.foo4(1));\n"
5430 " Expect.equals(12, obj.bar41(1));\n"
5431 " Expect.equals(3, Test.foo4(1, 1));\n"
5432 " Expect.equals(3, obj.bar42(1, 1));\n"
5433 " Expect.equals(6, Test.foo4(2, 2, 2));\n"
5434 " Expect.equals(6, obj.bar43(2, 2, 2));\n"
5435 "\n"
5436 " return 0;\n"
5437 "}\n";
5438
5439 Dart_Handle result;
5440
5441 // Load a test script.
5442 Dart_Handle url = Dart_NewString(TestCase::url());
5443 Dart_Handle source = Dart_NewString(kScriptChars);
5444 result = Dart_SetLibraryTagHandler(library_handler);
5445 EXPECT_VALID(result);
5446 Dart_Handle lib = Dart_LoadScript(url, source);
5447 EXPECT_VALID(lib);
5448 EXPECT(Dart_IsLibrary(lib));
5449 result = Dart_SetNativeResolver(lib, &MyStaticNativeClosureResolver);
5450 EXPECT_VALID(result);
5451
5452 result = Dart_Invoke(lib, Dart_NewString("testMain"), 0, NULL);
5453 EXPECT_VALID(result);
5454 EXPECT(Dart_IsInteger(result));
5455 int64_t value = 0;
5456 EXPECT_VALID(Dart_IntegerToInt64(result, &value));
5457 EXPECT_EQ(0, value);
5458 }
5459
5175 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 5460 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
5176 5461
5177 } // namespace dart 5462 } // namespace dart
OLDNEW
« no previous file with comments | « vm/code_patcher_x64_test.cc ('k') | vm/flow_graph_compiler_ia32.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698