Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 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 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 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 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" | |
|
regis
2012/06/15 17:13:34
Add a test for a static native.
siva
2012/06/16 00:25:43
Done.
| |
| 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 | |
| 5175 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 5322 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 5176 | 5323 |
| 5177 } // namespace dart | 5324 } // namespace dart |
| OLD | NEW |