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

Side by Side Diff: runtime/vm/dart_api_impl_test.cc

Issue 1499853004: Adds a special case for sending an int over a port with the native API. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Cleanup Created 5 years 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
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 "bin/builtin.h" 5 #include "bin/builtin.h"
6 #include "include/dart_api.h" 6 #include "include/dart_api.h"
7 #include "include/dart_mirrors_api.h" 7 #include "include/dart_mirrors_api.h"
8 #include "include/dart_native_api.h" 8 #include "include/dart_native_api.h"
9 #include "include/dart_tools_api.h" 9 #include "include/dart_tools_api.h"
10 #include "platform/assert.h" 10 #include "platform/assert.h"
(...skipping 6962 matching lines...) Expand 10 before | Expand all | Expand 10 after
6973 response->value.as_int32 = 123; 6973 response->value.as_int32 = 123;
6974 Dart_PostCObject( 6974 Dart_PostCObject(
6975 message->value.as_array.values[0]->value.as_send_port.id, response); 6975 message->value.as_array.values[0]->value.as_send_port.id, response);
6976 } 6976 }
6977 6977
6978 6978
6979 void NewNativePort_send321(Dart_Port dest_port_id, 6979 void NewNativePort_send321(Dart_Port dest_port_id,
6980 Dart_CObject* message) { 6980 Dart_CObject* message) {
6981 // Gets a null message. 6981 // Gets a null message.
6982 EXPECT_NOTNULL(message); 6982 EXPECT_NOTNULL(message);
6983 EXPECT_EQ(Dart_CObject_kArray, message->type);
6983 EXPECT_EQ(Dart_CObject_kSendPort, message->value.as_array.values[0]->type); 6984 EXPECT_EQ(Dart_CObject_kSendPort, message->value.as_array.values[0]->type);
6984 6985
6985 // Post integer value. 6986 // Post integer value.
6986 Dart_CObject* response = 6987 Dart_CObject* response =
6987 reinterpret_cast<Dart_CObject*>(Dart_ScopeAllocate(sizeof(Dart_CObject))); 6988 reinterpret_cast<Dart_CObject*>(Dart_ScopeAllocate(sizeof(Dart_CObject)));
6988 response->type = Dart_CObject_kInt32; 6989 response->type = Dart_CObject_kInt32;
6989 response->value.as_int32 = 321; 6990 response->value.as_int32 = 321;
6990 Dart_PostCObject( 6991 Dart_PostCObject(
6991 message->value.as_array.values[0]->value.as_send_port.id, response); 6992 message->value.as_array.values[0]->value.as_send_port.id, response);
6992 } 6993 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
7060 EXPECT_SUBSTRING("Exception: 321\n", Dart_GetError(result)); 7061 EXPECT_SUBSTRING("Exception: 321\n", Dart_GetError(result));
7061 7062
7062 Dart_ExitScope(); 7063 Dart_ExitScope();
7063 7064
7064 // Delete the native ports. 7065 // Delete the native ports.
7065 EXPECT(Dart_CloseNativePort(port_id1)); 7066 EXPECT(Dart_CloseNativePort(port_id1));
7066 EXPECT(Dart_CloseNativePort(port_id2)); 7067 EXPECT(Dart_CloseNativePort(port_id2));
7067 } 7068 }
7068 7069
7069 7070
7071 void NewNativePort_sendInteger123(Dart_Port dest_port_id,
7072 Dart_CObject *message) {
7073 // Gets a send port message.
7074 EXPECT_NOTNULL(message);
7075 EXPECT_EQ(Dart_CObject_kArray, message->type);
7076 EXPECT_EQ(Dart_CObject_kSendPort, message->value.as_array.values[0]->type);
7077
7078 // Post integer value.
7079 Dart_PostInteger(
7080 message->value.as_array.values[0]->value.as_send_port.id, 123);
7081 }
7082
7083
7084 void NewNativePort_sendInteger321(Dart_Port dest_port_id,
7085 Dart_CObject* message) {
7086 // Gets a null message.
7087 EXPECT_NOTNULL(message);
7088 EXPECT_EQ(Dart_CObject_kArray, message->type);
7089 EXPECT_EQ(Dart_CObject_kSendPort, message->value.as_array.values[0]->type);
7090
7091 // Post integer value.
7092 Dart_PostInteger(
7093 message->value.as_array.values[0]->value.as_send_port.id, 321);
7094 }
7095
7096
7097 TEST_CASE(NativePortPostInteger) {
7098 const char* kScriptChars =
7099 "import 'dart:isolate';\n"
7100 "void callPort(SendPort port) {\n"
7101 " var receivePort = new RawReceivePort();\n"
7102 " var replyPort = receivePort.sendPort;\n"
7103 " port.send([replyPort]);\n"
7104 " receivePort.handler = (message) {\n"
7105 " receivePort.close();\n"
7106 " throw new Exception(message);\n"
7107 " };\n"
7108 "}\n";
7109 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
7110 Dart_EnterScope();
7111
7112 Dart_Port port_id1 =
7113 Dart_NewNativePort("Port123", NewNativePort_sendInteger123, true);
7114 Dart_Port port_id2 =
7115 Dart_NewNativePort("Port321", NewNativePort_sendInteger321, true);
7116
7117 Dart_Handle send_port1 = Dart_NewSendPort(port_id1);
7118 EXPECT_VALID(send_port1);
7119 Dart_Handle send_port2 = Dart_NewSendPort(port_id2);
7120 EXPECT_VALID(send_port2);
7121
7122 // Test first port.
7123 Dart_Handle dart_args[1];
7124 dart_args[0] = send_port1;
7125 Dart_Handle result =
7126 Dart_Invoke(lib, NewString("callPort"), 1, dart_args);
7127 EXPECT_VALID(result);
7128 result = Dart_RunLoop();
7129 EXPECT(Dart_IsError(result));
7130 EXPECT(Dart_ErrorHasException(result));
7131 EXPECT_SUBSTRING("Exception: 123\n", Dart_GetError(result));
7132
7133 // result second port.
7134 dart_args[0] = send_port2;
7135 result = Dart_Invoke(lib, NewString("callPort"), 1, dart_args);
7136 EXPECT_VALID(result);
7137 result = Dart_RunLoop();
7138 EXPECT(Dart_IsError(result));
7139 EXPECT(Dart_ErrorHasException(result));
7140 EXPECT_SUBSTRING("Exception: 321\n", Dart_GetError(result));
7141
7142 Dart_ExitScope();
7143
7144 // Delete the native ports.
7145 EXPECT(Dart_CloseNativePort(port_id1));
7146 EXPECT(Dart_CloseNativePort(port_id2));
7147 }
7148
7149
7150 void NewNativePort_nativeReceiveNull(Dart_Port dest_port_id,
7151 Dart_CObject *message) {
7152 EXPECT_NOTNULL(message);
7153
7154 if ((message->type == Dart_CObject_kArray) &&
7155 (message->value.as_array.values[0]->type == Dart_CObject_kSendPort)) {
7156 // Post integer value.
7157 Dart_PostInteger(
7158 message->value.as_array.values[0]->value.as_send_port.id, 123);
7159 } else {
7160 EXPECT_EQ(message->type, Dart_CObject_kNull);
7161 }
7162 }
7163
7164
7165 TEST_CASE(NativePortReceiveNull) {
7166 const char* kScriptChars =
7167 "import 'dart:isolate';\n"
7168 "void callPort(SendPort port) {\n"
7169 " var receivePort = new RawReceivePort();\n"
7170 " var replyPort = receivePort.sendPort;\n"
7171 " port.send(null);\n"
7172 " port.send([replyPort]);\n"
7173 " receivePort.handler = (message) {\n"
7174 " receivePort.close();\n"
7175 " throw new Exception(message);\n"
7176 " };\n"
7177 "}\n";
7178 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
7179 Dart_EnterScope();
7180
7181 Dart_Port port_id1 =
7182 Dart_NewNativePort("PortNull", NewNativePort_nativeReceiveNull, true);
7183 Dart_Handle send_port1 = Dart_NewSendPort(port_id1);
7184 EXPECT_VALID(send_port1);
7185
7186 // Test first port.
7187 Dart_Handle dart_args[1];
7188 dart_args[0] = send_port1;
7189 Dart_Handle result =
7190 Dart_Invoke(lib, NewString("callPort"), 1, dart_args);
7191 EXPECT_VALID(result);
7192 result = Dart_RunLoop();
7193 EXPECT(Dart_IsError(result));
7194 EXPECT(Dart_ErrorHasException(result));
7195 EXPECT_SUBSTRING("Exception: 123\n", Dart_GetError(result));
7196
7197 Dart_ExitScope();
7198
7199 // Delete the native ports.
7200 EXPECT(Dart_CloseNativePort(port_id1));
7201 }
7202
7203
7204 void NewNativePort_nativeReceiveInteger(Dart_Port dest_port_id,
7205 Dart_CObject *message) {
7206 EXPECT_NOTNULL(message);
7207
7208 if ((message->type == Dart_CObject_kArray) &&
7209 (message->value.as_array.values[0]->type == Dart_CObject_kSendPort)) {
7210 // Post integer value.
7211 Dart_PostInteger(
7212 message->value.as_array.values[0]->value.as_send_port.id, 123);
7213 } else {
7214 EXPECT_EQ(message->type, Dart_CObject_kInt32);
7215 EXPECT_EQ(message->value.as_int32, 321);
7216 }
7217 }
7218
7219
7220 TEST_CASE(NativePortReceiveInteger) {
7221 const char* kScriptChars =
7222 "import 'dart:isolate';\n"
7223 "void callPort(SendPort port) {\n"
7224 " var receivePort = new RawReceivePort();\n"
7225 " var replyPort = receivePort.sendPort;\n"
7226 " port.send(321);\n"
7227 " port.send([replyPort]);\n"
7228 " receivePort.handler = (message) {\n"
7229 " receivePort.close();\n"
7230 " throw new Exception(message);\n"
7231 " };\n"
7232 "}\n";
7233 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
7234 Dart_EnterScope();
7235
7236 Dart_Port port_id1 =
7237 Dart_NewNativePort("PortNull", NewNativePort_nativeReceiveInteger, true);
7238 Dart_Handle send_port1 = Dart_NewSendPort(port_id1);
7239 EXPECT_VALID(send_port1);
7240
7241 // Test first port.
7242 Dart_Handle dart_args[1];
7243 dart_args[0] = send_port1;
7244 Dart_Handle result =
7245 Dart_Invoke(lib, NewString("callPort"), 1, dart_args);
7246 EXPECT_VALID(result);
7247 result = Dart_RunLoop();
7248 EXPECT(Dart_IsError(result));
7249 EXPECT(Dart_ErrorHasException(result));
7250 EXPECT_SUBSTRING("Exception: 123\n", Dart_GetError(result));
7251
7252 Dart_ExitScope();
7253
7254 // Delete the native ports.
7255 EXPECT(Dart_CloseNativePort(port_id1));
7256 }
7257
7258
7070 static Dart_Isolate RunLoopTestCallback(const char* script_name, 7259 static Dart_Isolate RunLoopTestCallback(const char* script_name,
7071 const char* main, 7260 const char* main,
7072 const char* package_root, 7261 const char* package_root,
7073 const char** package_map, 7262 const char** package_map,
7074 Dart_IsolateFlags* flags, 7263 Dart_IsolateFlags* flags,
7075 void* data, 7264 void* data,
7076 char** error) { 7265 char** error) {
7077 const char* kScriptChars = 7266 const char* kScriptChars =
7078 "import 'builtin';\n" 7267 "import 'builtin';\n"
7079 "import 'dart:isolate';\n" 7268 "import 'dart:isolate';\n"
(...skipping 2291 matching lines...) Expand 10 before | Expand all | Expand 10 after
9371 EXPECT_SUBSTRING("\"cat\":\"Compiler\"", buffer); 9560 EXPECT_SUBSTRING("\"cat\":\"Compiler\"", buffer);
9372 EXPECT_SUBSTRING("\"name\":\"CompileFunction\"", buffer); 9561 EXPECT_SUBSTRING("\"name\":\"CompileFunction\"", buffer);
9373 EXPECT_SUBSTRING("\"function\":\"::_main\"", buffer); 9562 EXPECT_SUBSTRING("\"function\":\"::_main\"", buffer);
9374 9563
9375 // Heartbeat test for new events. 9564 // Heartbeat test for new events.
9376 EXPECT_SUBSTRING("\"name\":\"TestVMDuration2\"", buffer); 9565 EXPECT_SUBSTRING("\"name\":\"TestVMDuration2\"", buffer);
9377 EXPECT_SUBSTRING("\"function\":\"::_bar\"", buffer); 9566 EXPECT_SUBSTRING("\"function\":\"::_bar\"", buffer);
9378 } 9567 }
9379 9568
9380 } // namespace dart 9569 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698