Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 static void functionInvocationCallback(const v8::FunctionCallbackInfo<v8::Value> & args) | 76 static void functionInvocationCallback(const v8::FunctionCallbackInfo<v8::Value> & args) |
| 77 { | 77 { |
| 78 DartScopes scopes(args.Holder()); | 78 DartScopes scopes(args.Holder()); |
| 79 Dart_Handle handle = scopes.handle; | 79 Dart_Handle handle = scopes.handle; |
| 80 DartDOMData* domData = DartDOMData::current(); | 80 DartDOMData* domData = DartDOMData::current(); |
| 81 ASSERT(domData); | 81 ASSERT(domData); |
| 82 ASSERT(DartUtilities::isFunction(domData, handle)); | 82 ASSERT(DartUtilities::isFunction(domData, handle)); |
| 83 ASSERT(args.Length() == 1 || args.Length() == 2); | |
| 83 | 84 |
| 84 Vector<Dart_Handle> dartFunctionArgs; | 85 v8::Local<v8::Array> argsList = args[args.Length()-1].As<v8::Array>(); |
| 85 ASSERT(args.Length() == 1 || args.Length() == 2); | 86 uint32_t argsListLength = argsList->Length(); |
| 87 Dart_Handle dartFunctionArgs; | |
| 88 uint32_t offset = 0; | |
| 86 // If there is 1 argument, we assume it is a v8:Array or arguments, if | 89 // If there is 1 argument, we assume it is a v8:Array or arguments, if |
| 87 // there are 2 arguments, the first argument is "this" and the second | 90 // there are 2 arguments, the first argument is "this" and the second |
| 88 // argument is an array of arguments. | 91 // argument is an array of arguments. |
| 89 if (args.Length() > 1) { | 92 if (args.Length() == 1) { |
| 90 dartFunctionArgs.append(JsInterop::toDart(args[0])); | 93 dartFunctionArgs = Dart_NewList(argsListLength); |
| 94 } else { | |
| 95 dartFunctionArgs = Dart_NewList(argsListLength + 1); | |
| 96 Dart_ListSetAt(dartFunctionArgs, offset++, JsInterop::toDart(args[0])); | |
| 91 } | 97 } |
| 92 | 98 |
| 93 v8::Local<v8::Array> argsList = args[args.Length()-1].As<v8::Array>(); | |
| 94 uint32_t argsListLength = argsList->Length(); | |
| 95 for (uint32_t i = 0; i < argsListLength; i++) { | 99 for (uint32_t i = 0; i < argsListLength; i++) { |
| 96 dartFunctionArgs.append(JsInterop::toDart(argsList->Get(i))); | 100 Dart_ListSetAt(dartFunctionArgs, i + offset, JsInterop::toDart(argsList- >Get(i))); |
|
Jacob
2014/04/16 22:27:55
nit: keep the style consistent and replace
i + off
| |
| 97 } | 101 } |
| 98 | 102 |
| 99 setJsReturnValue(domData, args, Dart_InvokeClosure(handle, dartFunctionArgs. size(), dartFunctionArgs.data())); | 103 setJsReturnValue(domData, args, Dart_InvokeClosure(handle, 1, &dartFunctionA rgs)); |
| 100 } | 104 } |
| 101 | 105 |
| 102 static v8::Local<v8::ObjectTemplate> setupInstanceTemplate(v8::Local<v8::Functio nTemplate> proxyTemplate) | 106 static v8::Local<v8::ObjectTemplate> setupInstanceTemplate(v8::Local<v8::Functio nTemplate> proxyTemplate) |
| 103 { | 107 { |
| 104 v8::Local<v8::ObjectTemplate> instanceTemplate = proxyTemplate->InstanceTemp late(); | 108 v8::Local<v8::ObjectTemplate> instanceTemplate = proxyTemplate->InstanceTemp late(); |
| 105 instanceTemplate->SetInternalFieldCount(1); | 109 instanceTemplate->SetInternalFieldCount(1); |
| 106 return instanceTemplate; | 110 return instanceTemplate; |
| 107 } | 111 } |
| 108 | 112 |
| 109 static v8::Local<v8::FunctionTemplate> dartFunctionTemplate() | 113 static v8::Local<v8::FunctionTemplate> dartFunctionTemplate() |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 if (!value.IsEmpty() || exception) | 213 if (!value.IsEmpty() || exception) |
| 210 return value; | 214 return value; |
| 211 | 215 |
| 212 if (DartDOMWrapper::subtypeOf(handle, JsObject::dartClassId)) { | 216 if (DartDOMWrapper::subtypeOf(handle, JsObject::dartClassId)) { |
| 213 JsObject* object = DartDOMWrapper::unwrapDartWrapper<JsObject>(domData, handle, exception); | 217 JsObject* object = DartDOMWrapper::unwrapDartWrapper<JsObject>(domData, handle, exception); |
| 214 if (exception) | 218 if (exception) |
| 215 return v8::Local<v8::Value>(); | 219 return v8::Local<v8::Value>(); |
| 216 return object->localV8Object(); | 220 return object->localV8Object(); |
| 217 } | 221 } |
| 218 | 222 |
| 219 if (DartUtilities::isFunction(domData, handle)) { | 223 if (DartUtilities::isFunction(domData, handle)) { |
|
Jacob
2014/04/16 22:27:55
add a TODO to remove this isFunction check and swi
| |
| 220 v8::Local<v8::Object> functionProxy = dartFunctionTemplate()->InstanceTe mplate()->NewInstance(); | 224 v8::Local<v8::Object> functionProxy = dartFunctionTemplate()->InstanceTe mplate()->NewInstance(); |
| 221 DartHandleProxy::writePointerToProxy(functionProxy, handle); | 225 DartHandleProxy::writePointerToProxy(functionProxy, handle); |
| 222 // The raw functionProxy doesn't behave enough like a true JS function | 226 // The raw functionProxy doesn't behave enough like a true JS function |
| 223 // so we wrap it in a true JS function. | 227 // so we wrap it in a true JS function. |
| 224 return domData->jsInteropData()->wrapDartFunction()->Call(functionProxy, 0, 0); | 228 v8::Local<v8::Function> jsFunction = v8::Local<v8::Function>::Cast(domDa ta->jsInteropData()->wrapDartFunction()->Call(functionProxy, 0, 0)); |
| 229 DartHandleProxy::writePointerToProxy(jsFunction, handle); | |
| 230 v8::Isolate* v8Isolate = v8::Isolate::GetCurrent(); | |
| 231 jsFunction->SetHiddenValue(v8::String::NewFromUtf8(v8Isolate, "dartProxy "), v8::Boolean::New(v8Isolate, true)); | |
| 232 return jsFunction; | |
| 225 } | 233 } |
| 226 | 234 |
| 227 v8::Local<v8::Object> proxy; | 235 v8::Local<v8::Object> proxy; |
| 228 ASSERT(Dart_IsInstance(handle)); | 236 ASSERT(Dart_IsInstance(handle)); |
| 229 proxy = dartObjectTemplate()->InstanceTemplate()->NewInstance(); | 237 proxy = dartObjectTemplate()->InstanceTemplate()->NewInstance(); |
| 230 DartHandleProxy::writePointerToProxy(proxy, handle); | 238 DartHandleProxy::writePointerToProxy(proxy, handle); |
| 231 v8::Isolate* v8Isolate = v8::Isolate::GetCurrent(); | 239 v8::Isolate* v8Isolate = v8::Isolate::GetCurrent(); |
| 232 proxy->SetHiddenValue(v8::String::NewFromUtf8(v8Isolate, "dartProxy"), v8::B oolean::New(v8Isolate, true)); | 240 proxy->SetHiddenValue(v8::String::NewFromUtf8(v8Isolate, "dartProxy"), v8::B oolean::New(v8Isolate, true)); |
| 233 | 241 |
| 234 return proxy; | 242 return proxy; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 253 return handle; | 261 return handle; |
| 254 | 262 |
| 255 ASSERT(v8Handle->IsObject()); | 263 ASSERT(v8Handle->IsObject()); |
| 256 v8::Handle<v8::Object> object = v8Handle.As<v8::Object>(); | 264 v8::Handle<v8::Object> object = v8Handle.As<v8::Object>(); |
| 257 Dart_Handle exception = 0; | 265 Dart_Handle exception = 0; |
| 258 handle = V8Converter::toDartIfBrowserNative(object, object->CreationContext( )->GetIsolate(), exception); | 266 handle = V8Converter::toDartIfBrowserNative(object, object->CreationContext( )->GetIsolate(), exception); |
| 259 ASSERT(!exception); | 267 ASSERT(!exception); |
| 260 if (handle) | 268 if (handle) |
| 261 return handle; | 269 return handle; |
| 262 | 270 |
| 263 // Unwrap objects passed from Dart to JS that are being passed back to | 271 // Unwrap objects passed from Dart to JS that are being passed back to Dart. |
| 264 // Dart. FIXME: we do not yet handle unwrapping JS functions passed | |
| 265 // from Dart to JS as we have to wrap them with true JS Function objects. | |
| 266 // If this use case is important we can support it at the cost of hanging | |
| 267 // an extra expando off the JS function wrapping the Dart function. | |
| 268 if (DartHandleProxy::isDartProxy(v8Handle)) { | 272 if (DartHandleProxy::isDartProxy(v8Handle)) { |
| 269 DartScriptValue* scriptValue = DartHandleProxy::readPointerFromProxy(v8H andle); | 273 DartScriptValue* scriptValue = DartHandleProxy::readPointerFromProxy(v8H andle); |
| 270 ASSERT(scriptValue->isIsolateAlive()); | 274 ASSERT(scriptValue->isIsolateAlive()); |
| 271 return scriptValue->value(); | 275 return scriptValue->value(); |
| 272 } | 276 } |
| 273 | 277 |
| 274 return JsObject::toDart(object); | 278 return JsObject::toDart(object); |
| 275 } | 279 } |
| 276 | 280 |
| 277 Dart_Handle JsObject::toDart(v8::Local<v8::Object> object) | 281 Dart_Handle JsObject::toDart(v8::Local<v8::Object> object) |
| (...skipping 770 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1048 if (argumentCount == 1 && name == "JsArray_length") | 1052 if (argumentCount == 1 && name == "JsArray_length") |
| 1049 return JsInteropInternal::jsArrayLengthCallback; | 1053 return JsInteropInternal::jsArrayLengthCallback; |
| 1050 | 1054 |
| 1051 if (argumentCount == 1 && name == "JsObject_fromBrowserObject") | 1055 if (argumentCount == 1 && name == "JsObject_fromBrowserObject") |
| 1052 return JsInteropInternal::fromBrowserObjectCallback; | 1056 return JsInteropInternal::fromBrowserObjectCallback; |
| 1053 | 1057 |
| 1054 return 0; | 1058 return 0; |
| 1055 } | 1059 } |
| 1056 | 1060 |
| 1057 } | 1061 } |
| OLD | NEW |