diff --git a/bridge/bindings/qjs/binding_initializer.cc b/bridge/bindings/qjs/binding_initializer.cc index 20fc0b15cb..af56625f52 100644 --- a/bridge/bindings/qjs/binding_initializer.cc +++ b/bridge/bindings/qjs/binding_initializer.cc @@ -1,11 +1,10 @@ /* -* Copyright (C) 2019 Alibaba Inc. All rights reserved. -* Author: Kraken Team. -*/ + * Copyright (C) 2019 Alibaba Inc. All rights reserved. + * Author: Kraken Team. + */ #include "binding_initializer.h" - //#include "bindings/qjs/bom/blob.h" //#include "bindings/qjs/bom/console.h" //#include "bindings/qjs/bom/location.h" @@ -42,8 +41,6 @@ namespace kraken { -void initBinding() { - -} +void initBinding() {} -} +} // namespace kraken diff --git a/bridge/bindings/qjs/binding_initializer.h b/bridge/bindings/qjs/binding_initializer.h index 87e3d686c6..919eab4013 100644 --- a/bridge/bindings/qjs/binding_initializer.h +++ b/bridge/bindings/qjs/binding_initializer.h @@ -1,7 +1,7 @@ /* -* Copyright (C) 2019 Alibaba Inc. All rights reserved. -* Author: Kraken Team. -*/ + * Copyright (C) 2019 Alibaba Inc. All rights reserved. + * Author: Kraken Team. + */ #ifndef KRAKENBRIDGE_BINDING_INITIALIZER_H #define KRAKENBRIDGE_BINDING_INITIALIZER_H @@ -10,7 +10,6 @@ namespace kraken { void initBinding(); - // bindConsole(m_context); // bindTimer(m_context); // bindScreen(m_context); @@ -46,6 +45,6 @@ void initBinding(); // bindDocument(m_context); // bindPerformance(m_context); -} +} // namespace kraken #endif // KRAKENBRIDGE_BINDING_INITIALIZER_H diff --git a/bridge/bindings/qjs/native_string_utils.cc b/bridge/bindings/qjs/native_string_utils.cc index 3f726022f4..aaec1d3287 100644 --- a/bridge/bindings/qjs/native_string_utils.cc +++ b/bridge/bindings/qjs/native_string_utils.cc @@ -30,7 +30,6 @@ std::unique_ptr jsValueToNativeString(JSContext* ctx, JSValue valu return ptr; } - std::unique_ptr stringToNativeString(const std::string& string) { std::u16string utf16; fromUTF8(string, utf16); @@ -61,6 +60,4 @@ std::string jsAtomToStdString(JSContext* ctx, JSAtom atom) { return str; } - - -} +} // namespace kraken diff --git a/bridge/bindings/qjs/native_string_utils.h b/bridge/bindings/qjs/native_string_utils.h index 5677807fe3..309c734e8e 100644 --- a/bridge/bindings/qjs/native_string_utils.h +++ b/bridge/bindings/qjs/native_string_utils.h @@ -7,10 +7,10 @@ #define KRAKENBRIDGE_NATIVE_STRING_UTILS_H #include -#include -#include -#include #include +#include +#include +#include #include "foundation/native_string.h" @@ -47,6 +47,6 @@ void fromUTF8(const std::string& source, std::basic_string -#include "event_target.h" #include "bindings/qjs/qjs_patch.h" -#include "custom_event.h" -#include "event.h" #include "core/dom/node.h" #include "core/frame/window.h" +#include "custom_event.h" +#include "event.h" +#include "event_target.h" #if UNIT_TEST #include "kraken_test_env.h" diff --git a/bridge/core/dom/events/event_target.h b/bridge/core/dom/events/event_target.h index cdcd0f76e9..2246c8afb2 100644 --- a/bridge/core/dom/events/event_target.h +++ b/bridge/core/dom/events/event_target.h @@ -6,11 +6,11 @@ #ifndef KRAKENBRIDGE_EVENT_TARGET_H #define KRAKENBRIDGE_EVENT_TARGET_H -#include "foundation/macros.h" -#include "bindings/qjs/macros.h" #include "bindings/qjs/heap_hashmap.h" +#include "bindings/qjs/macros.h" #include "core/executing_context.h" #include "event_listener_map.h" +#include "foundation/macros.h" #if UNIT_TEST void TEST_callNativeMethod(void* nativePtr, void* returnValue, void* method, int32_t argc, void* argv); diff --git a/bridge/core/executing_context.cc b/bridge/core/executing_context.cc index dbf820edba..a856d97cb0 100644 --- a/bridge/core/executing_context.cc +++ b/bridge/core/executing_context.cc @@ -70,32 +70,32 @@ ExecutionContext::~ExecutionContext() { ctxInvalid_ = true; // Manual free nodes bound by each other. -// { -// struct list_head *el, *el1; -// list_for_each_safe(el, el1, &node_job_list) { -// auto* node = list_entry(el, NodeJob, link); -// JS_FreeValue(m_ctx, node->nodeInstance->jsObject); -// } -// } -// -// // Manual free moduleListener -// { -// struct list_head *el, *el1; -// list_for_each_safe(el, el1, &module_job_list) { -// auto* module = list_entry(el, ModuleContext, link); -// JS_FreeValue(m_ctx, module->callback); -// delete module; -// } -// } -// -// { -// struct list_head *el, *el1; -// list_for_each_safe(el, el1, &module_callback_job_list) { -// auto* module = list_entry(el, ModuleContext, link); -// JS_FreeValue(m_ctx, module->callback); -// delete module; -// } -// } + // { + // struct list_head *el, *el1; + // list_for_each_safe(el, el1, &node_job_list) { + // auto* node = list_entry(el, NodeJob, link); + // JS_FreeValue(m_ctx, node->nodeInstance->jsObject); + // } + // } + // + // // Manual free moduleListener + // { + // struct list_head *el, *el1; + // list_for_each_safe(el, el1, &module_job_list) { + // auto* module = list_entry(el, ModuleContext, link); + // JS_FreeValue(m_ctx, module->callback); + // delete module; + // } + // } + // + // { + // struct list_head *el, *el1; + // list_for_each_safe(el, el1, &module_callback_job_list) { + // auto* module = list_entry(el, ModuleContext, link); + // JS_FreeValue(m_ctx, module->callback); + // delete module; + // } + // } // Free unresolved promise. { @@ -369,7 +369,6 @@ void buildUICommandArgs(JSContext* ctx, JSValue key, NativeString& args_01) { args_01.length = length; } - // An lock free context validator. bool isContextValid(int32_t contextId) { if (contextId > running_context_list) diff --git a/bridge/core/executing_context.h b/bridge/core/executing_context.h index fff00c4152..e06cee6694 100644 --- a/bridge/core/executing_context.h +++ b/bridge/core/executing_context.h @@ -16,13 +16,13 @@ #include #include #include +#include "bindings/qjs/garbage_collected.h" #include "foundation/macros.h" #include "foundation/ui_command_buffer.h" -#include "bindings/qjs/garbage_collected.h" +#include "dart_methods.h" #include "executing_context_data.h" #include "frame/dom_timer_coordinator.h" -#include "dart_methods.h" using JSExceptionHandler = std::function; diff --git a/bridge/core/executing_context_data.h b/bridge/core/executing_context_data.h index a24cd43e8f..90e612df4f 100644 --- a/bridge/core/executing_context_data.h +++ b/bridge/core/executing_context_data.h @@ -6,8 +6,8 @@ #ifndef KRAKENBRIDGE_CONTEXT_DATA_H #define KRAKENBRIDGE_CONTEXT_DATA_H -#include #include +#include #include "bindings/qjs/wrapper_type_info.h" namespace kraken { diff --git a/bridge/core/fileapi/blob.h b/bridge/core/fileapi/blob.h index dd4dfd98f0..cd63e0a5db 100644 --- a/bridge/core/fileapi/blob.h +++ b/bridge/core/fileapi/blob.h @@ -6,8 +6,8 @@ #ifndef KRAKENBRIDGE_BLOB_H #define KRAKENBRIDGE_BLOB_H -#include "bindings/qjs/macros.h" #include "bindings/qjs/garbage_collected.h" +#include "bindings/qjs/macros.h" namespace kraken { diff --git a/bridge/core/html/html_all_collection.cc b/bridge/core/html/html_all_collection.cc index 98318d2d82..0f6894bc77 100644 --- a/bridge/core/html/html_all_collection.cc +++ b/bridge/core/html/html_all_collection.cc @@ -5,9 +5,9 @@ // //#include "html_all_collection.h" // -//namespace kraken { +// namespace kraken { // -//JSValue AllCollection::item(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// JSValue AllCollection::item(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // if (argc < 1) { // return JS_NULL; // } @@ -23,7 +23,7 @@ // auto node = collection->m_nodes[index]; // return node->jsObject; //} -//JSValue AllCollection::add(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// JSValue AllCollection::add(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // if (argc < 1) { // return JS_ThrowTypeError(ctx, "Failed to execute add() on HTMLAllCollection: 1 arguments required."); // } @@ -50,7 +50,7 @@ // // return JS_NULL; //} -//JSValue AllCollection::remove(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// JSValue AllCollection::remove(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // if (argc < 1) { // return JS_ThrowTypeError(ctx, "Failed to execute remove() on HTMLAllCollection: 1 arguments required."); // } @@ -61,7 +61,7 @@ // collection->m_nodes.erase(collection->m_nodes.begin() + index); // return JS_NULL; //} -//void AllCollection::internalAdd(NodeInstance* node, NodeInstance* before) { +// void AllCollection::internalAdd(NodeInstance* node, NodeInstance* before) { // if (before != nullptr) { // auto it = std::find(m_nodes.begin(), m_nodes.end(), before); // m_nodes.erase(it); @@ -71,7 +71,7 @@ // } //} // -//IMPL_PROPERTY_GETTER(AllCollection, length)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_GETTER(AllCollection, length)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // auto* collection = static_cast(JS_GetOpaque(this_val, ExecutionContext::kHostObjectClassId)); // return JS_NewUint32(ctx, collection->m_nodes.size()); //} diff --git a/bridge/core/html/html_all_collection.h b/bridge/core/html/html_all_collection.h index 9946cd154a..9483d30fd0 100644 --- a/bridge/core/html/html_all_collection.h +++ b/bridge/core/html/html_all_collection.h @@ -8,9 +8,9 @@ // //#include "bindings/qjs/garbage_collected.h" // -//namespace kraken { +// namespace kraken { // -//class HTMLAllCollection : public HostObject { +// class HTMLAllCollection : public HostObject { // public: // AllCollection(ExecutionContext* context) : HostObject(context, "AllCollection"){}; // diff --git a/bridge/core/html/html_image_element.cc b/bridge/core/html/html_image_element.cc index 874d50634a..a7017caf2b 100644 --- a/bridge/core/html/html_image_element.cc +++ b/bridge/core/html/html_image_element.cc @@ -7,28 +7,28 @@ //#include "bindings/qjs/qjs_patch.h" //#include "page.h" // -//namespace kraken { +// namespace kraken { // -//ImageElement::ImageElement(ExecutionContext* context) : Element(context) { +// ImageElement::ImageElement(ExecutionContext* context) : Element(context) { // JS_SetPrototype(m_ctx, m_prototypeObject, Element::instance(m_context)->prototype()); //} // -//void bindImageElement(ExecutionContext* context) { +// void bindImageElement(ExecutionContext* context) { // auto* constructor = ImageElement::instance(context); // context->defineGlobalProperty("HTMLImageElement", constructor->jsObject); // context->defineGlobalProperty("Image", JS_DupValue(context->ctx(), constructor->jsObject)); //} // -//JSValue ImageElement::instanceConstructor(JSContext* ctx, JSValue func_obj, JSValue this_val, int argc, JSValue* argv) { +// JSValue ImageElement::instanceConstructor(JSContext* ctx, JSValue func_obj, JSValue this_val, int argc, JSValue* argv) { // auto instance = new ImageElementInstance(this); // return instance->jsObject; //} -//IMPL_PROPERTY_GETTER(ImageElement, width)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_GETTER(ImageElement, width)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // getDartMethod()->flushUICommand(); // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // return element->getNativeProperty("width"); //} -//IMPL_PROPERTY_SETTER(ImageElement, width)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_SETTER(ImageElement, width)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // std::string key = "width"; // std::unique_ptr args_01 = stringToNativeString(key); @@ -36,12 +36,12 @@ // element->m_context->uiCommandBuffer()->addCommand(element->m_eventTargetId, UICommand::setProperty, *args_01, *args_02, nullptr); // return JS_NULL; //} -//IMPL_PROPERTY_GETTER(ImageElement, height)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_GETTER(ImageElement, height)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // getDartMethod()->flushUICommand(); // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // return element->getNativeProperty("height"); //} -//IMPL_PROPERTY_SETTER(ImageElement, height)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_SETTER(ImageElement, height)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // std::string key = "height"; // std::unique_ptr args_01 = stringToNativeString(key); @@ -49,22 +49,22 @@ // element->m_context->uiCommandBuffer()->addCommand(element->m_eventTargetId, UICommand::setProperty, *args_01, *args_02, nullptr); // return JS_NULL; //} -//IMPL_PROPERTY_GETTER(ImageElement, naturalWidth)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_GETTER(ImageElement, naturalWidth)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // getDartMethod()->flushUICommand(); // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // return element->getNativeProperty("naturalWidth"); //} -//IMPL_PROPERTY_GETTER(ImageElement, naturalHeight)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_GETTER(ImageElement, naturalHeight)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // getDartMethod()->flushUICommand(); // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // return element->getNativeProperty("naturalHeight"); //} -//IMPL_PROPERTY_GETTER(ImageElement, src)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_GETTER(ImageElement, src)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // getDartMethod()->flushUICommand(); // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // return element->getNativeProperty("src"); //} -//IMPL_PROPERTY_SETTER(ImageElement, src)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_SETTER(ImageElement, src)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // std::string key = "src"; // std::unique_ptr args_01 = stringToNativeString(key); @@ -72,12 +72,12 @@ // element->m_context->uiCommandBuffer()->addCommand(element->m_eventTargetId, UICommand::setProperty, *args_01, *args_02, nullptr); // return JS_NULL; //} -//IMPL_PROPERTY_GETTER(ImageElement, loading)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_GETTER(ImageElement, loading)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // getDartMethod()->flushUICommand(); // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // return element->getNativeProperty("loading"); //} -//IMPL_PROPERTY_SETTER(ImageElement, loading)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { +// IMPL_PROPERTY_SETTER(ImageElement, loading)(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) { // auto* element = static_cast(JS_GetOpaque(this_val, Element::classId())); // std::string key = "loading"; // std::unique_ptr args_01 = stringToNativeString(key); @@ -86,12 +86,12 @@ // return JS_NULL; //} // -//ImageElementInstance::ImageElementInstance(ImageElement* element) : ElementInstance(element, "img", true) { +// ImageElementInstance::ImageElementInstance(ImageElement* element) : ElementInstance(element, "img", true) { // // Protect image instance util load or error event triggered. // refer(); //} // -//bool ImageElementInstance::dispatchEvent(EventInstance* event) { +// bool ImageElementInstance::dispatchEvent(EventInstance* event) { // std::u16string u16EventType = std::u16string(reinterpret_cast(event->nativeEvent->type->string), event->nativeEvent->type->length); // std::string eventType = toUTF8(u16EventType); // bool result = EventTargetInstance::dispatchEvent(event); diff --git a/bridge/core/html/html_image_element.h b/bridge/core/html/html_image_element.h index e1e2d051d4..4df787ea41 100644 --- a/bridge/core/html/html_image_element.h +++ b/bridge/core/html/html_image_element.h @@ -8,12 +8,12 @@ // //#include "bindings/qjs/dom/element.h" // -//namespace kraken { +// namespace kraken { // -//void bindImageElement(ExecutionContext* context); +// void bindImageElement(ExecutionContext* context); // -//class ImageElementInstance; -//class ImageElement : public Element { +// class ImageElementInstance; +// class ImageElement : public Element { // public: // ImageElement() = delete; // explicit ImageElement(ExecutionContext* context); @@ -32,7 +32,7 @@ // friend ImageElementInstance; //}; // -//class ImageElementInstance : public ElementInstance { +// class ImageElementInstance : public ElementInstance { // public: // ImageElementInstance() = delete; // explicit ImageElementInstance(ImageElement* element); diff --git a/bridge/foundation/logging.h b/bridge/foundation/logging.h index 5e2f47805a..2efe33d7af 100644 --- a/bridge/foundation/logging.h +++ b/bridge/foundation/logging.h @@ -33,18 +33,18 @@ constexpr LogSeverity LOG_NUM_SEVERITIES = 5; constexpr LogSeverity LOG_FATAL = 6; class LogMessageVoidify { -public: + public: void operator&(std::ostream&) {} }; class LogMessage { - public: + public: LogMessage(LogSeverity severity, const char* file, int line, const char* condition); ~LogMessage(); std::ostream& stream() { return stream_; } - private: + private: std::ostringstream stream_; const LogSeverity severity_; const char* file_; diff --git a/bridge/foundation/macros.h b/bridge/foundation/macros.h index a65cf1ec79..85ae551827 100644 --- a/bridge/foundation/macros.h +++ b/bridge/foundation/macros.h @@ -40,5 +40,4 @@ TypeName() = delete; \ KRAKEN_DISALLOW_COPY_ASSIGN_AND_MOVE(TypeName) - #endif // KRAKENBRIDGE_MACROS_H diff --git a/bridge/foundation/native_string.cc b/bridge/foundation/native_string.cc index 305d9da950..1df95b89c3 100644 --- a/bridge/foundation/native_string.cc +++ b/bridge/foundation/native_string.cc @@ -1,7 +1,7 @@ /* -* Copyright (C) 2021 Alibaba Inc. All rights reserved. -* Author: Kraken Team. -*/ + * Copyright (C) 2021 Alibaba Inc. All rights reserved. + * Author: Kraken Team. + */ #include "native_string.h" #include @@ -22,5 +22,4 @@ void NativeString::free() { delete[] string; } - -} +} // namespace kraken diff --git a/bridge/foundation/native_string.h b/bridge/foundation/native_string.h index bb86cc586d..5fd8ee84b1 100644 --- a/bridge/foundation/native_string.h +++ b/bridge/foundation/native_string.h @@ -1,7 +1,7 @@ /* -* Copyright (C) 2021 Alibaba Inc. All rights reserved. -* Author: Kraken Team. -*/ + * Copyright (C) 2021 Alibaba Inc. All rights reserved. + * Author: Kraken Team. + */ #ifndef KRAKENBRIDGE_NATIVE_STRING_H #define KRAKENBRIDGE_NATIVE_STRING_H @@ -18,6 +18,6 @@ struct NativeString { void free(); }; -} +} // namespace kraken #endif // KRAKENBRIDGE_NATIVE_STRING_H diff --git a/bridge/foundation/native_value.cc b/bridge/foundation/native_value.cc index 29787e524e..47cd2ad8a5 100644 --- a/bridge/foundation/native_value.cc +++ b/bridge/foundation/native_value.cc @@ -5,8 +5,8 @@ #include "native_value.h" #include "bindings/qjs/qjs_patch.h" -#include "core/executing_context.h" #include "core/dom/events/event_target.h" +#include "core/executing_context.h" namespace kraken { @@ -121,13 +121,13 @@ NativeValue jsValueToNativeValue(JSContext* ctx, JSValue& value) { auto* functionContext = new NativeFunctionContext{context, value}; return Native_NewPtr(JSPointerType::NativeFunctionContext, functionContext); } else if (JS_IsObject(value)) { -// auto* context = static_cast(JS_GetContextOpaque(ctx)); -// if (JS_IsInstanceOf(ctx, value, ImageElement::instance(context)->jsObject)) { -// auto* imageElementInstance = static_cast(JS_GetOpaque(value, Element::classId())); -// return Native_NewPtr(JSPointerType::NativeEventTarget, imageElementInstance->nativeEventTarget); -// } + // auto* context = static_cast(JS_GetContextOpaque(ctx)); + // if (JS_IsInstanceOf(ctx, value, ImageElement::instance(context)->jsObject)) { + // auto* imageElementInstance = static_cast(JS_GetOpaque(value, Element::classId())); + // return Native_NewPtr(JSPointerType::NativeEventTarget, imageElementInstance->nativeEventTarget); + // } -// return Native_NewJSON(context, value); + // return Native_NewJSON(context, value); } return Native_NewNull(); @@ -144,19 +144,19 @@ NativeFunctionContext::~NativeFunctionContext() { } static JSValue anonymousFunction(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* func_data) { -// auto id = magic; -// auto* eventTarget = static_cast(JS_GetOpaque(this_val, JSValueGetClassId(this_val))); -// -// std::string call_params = AnonymousFunctionCallPreFix + std::to_string(id); -// -// auto* arguments = new NativeValue[argc]; -// for (int i = 0; i < argc; i++) { -// arguments[i] = jsValueToNativeValue(ctx, argv[i]); -// } -// -// JSValue returnValue = eventTarget->callNativeMethods(call_params.c_str(), argc, arguments); -// delete[] arguments; -// return returnValue; + // auto id = magic; + // auto* eventTarget = static_cast(JS_GetOpaque(this_val, JSValueGetClassId(this_val))); + // + // std::string call_params = AnonymousFunctionCallPreFix + std::to_string(id); + // + // auto* arguments = new NativeValue[argc]; + // for (int i = 0; i < argc; i++) { + // arguments[i] = jsValueToNativeValue(ctx, argv[i]); + // } + // + // JSValue returnValue = eventTarget->callNativeMethods(call_params.c_str(), argc, arguments); + // delete[] arguments; + // return returnValue; } void anonymousAsyncCallback(void* callbackContext, NativeValue* nativeValue, int32_t contextId, const char* errmsg) { @@ -191,31 +191,31 @@ void anonymousAsyncCallback(void* callbackContext, NativeValue* nativeValue, int } static JSValue anonymousAsyncFunction(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* func_data) { -// JSValue resolving_funcs[2]; -// JSValue promise = JS_NewPromiseCapability(ctx, resolving_funcs); -// -// auto id = magic; -// auto* eventTarget = static_cast(JS_GetOpaque(this_val, JSValueGetClassId(this_val))); -// auto* context = eventTarget->context(); -// -// auto* promiseContext = new PromiseContext{eventTarget, context, resolving_funcs[0], resolving_funcs[1], promise}; -// list_add_tail(&promiseContext->link, &context->promise_job_list); -// -// std::string call_params = AsyncAnonymousFunctionCallPreFix + std::to_string(id); -// -// auto* arguments = new NativeValue[argc + 3]; -// -// arguments[0] = Native_NewInt32(context->getContextId()); -// arguments[1] = Native_NewPtr(JSPointerType::AsyncContextContext, promiseContext); -// arguments[2] = Native_NewPtr(JSPointerType::AsyncContextContext, reinterpret_cast(anonymousAsyncCallback)); -// for (int i = 0; i < argc; i++) { -// arguments[i + 3] = jsValueToNativeValue(ctx, argv[i]); -// } -// -// eventTarget->callNativeMethods(call_params.c_str(), argc + 3, arguments); -// delete[] arguments; -// -// return promise; + // JSValue resolving_funcs[2]; + // JSValue promise = JS_NewPromiseCapability(ctx, resolving_funcs); + // + // auto id = magic; + // auto* eventTarget = static_cast(JS_GetOpaque(this_val, JSValueGetClassId(this_val))); + // auto* context = eventTarget->context(); + // + // auto* promiseContext = new PromiseContext{eventTarget, context, resolving_funcs[0], resolving_funcs[1], promise}; + // list_add_tail(&promiseContext->link, &context->promise_job_list); + // + // std::string call_params = AsyncAnonymousFunctionCallPreFix + std::to_string(id); + // + // auto* arguments = new NativeValue[argc + 3]; + // + // arguments[0] = Native_NewInt32(context->getContextId()); + // arguments[1] = Native_NewPtr(JSPointerType::AsyncContextContext, promiseContext); + // arguments[2] = Native_NewPtr(JSPointerType::AsyncContextContext, reinterpret_cast(anonymousAsyncCallback)); + // for (int i = 0; i < argc; i++) { + // arguments[i + 3] = jsValueToNativeValue(ctx, argv[i]); + // } + // + // eventTarget->callNativeMethods(call_params.c_str(), argc + 3, arguments); + // delete[] arguments; + // + // return promise; } JSValue nativeValueToJSValue(ExecutionContext* context, NativeValue& value) { @@ -249,14 +249,14 @@ JSValue nativeValueToJSValue(ExecutionContext* context, NativeValue& value) { case NativeTag::TAG_POINTER: { auto* ptr = value.u.ptr; int ptrType = (int)value.float64; -// if (ptrType == static_cast(JSPointerType::NativeBoundingClientRect)) { -// return (new BoundingClientRect(context, static_cast(ptr)))->jsObject; -// } else if (ptrType == static_cast(JSPointerType::NativeCanvasRenderingContext2D)) { -// return (new CanvasRenderingContext2D(context, static_cast(ptr)))->jsObject; -// } else if (ptrType == static_cast(JSPointerType::NativeEventTarget)) { -// auto* nativeEventTarget = static_cast(ptr); -// return JS_DupValue(context->ctx(), nativeEventTarget->instance->jsObject); -// } + // if (ptrType == static_cast(JSPointerType::NativeBoundingClientRect)) { + // return (new BoundingClientRect(context, static_cast(ptr)))->jsObject; + // } else if (ptrType == static_cast(JSPointerType::NativeCanvasRenderingContext2D)) { + // return (new CanvasRenderingContext2D(context, static_cast(ptr)))->jsObject; + // } else if (ptrType == static_cast(JSPointerType::NativeEventTarget)) { + // auto* nativeEventTarget = static_cast(ptr); + // return JS_DupValue(context->ctx(), nativeEventTarget->instance->jsObject); + // } } case NativeTag::TAG_FUNCTION: { int64_t functionId = value.u.int64; @@ -270,4 +270,4 @@ JSValue nativeValueToJSValue(ExecutionContext* context, NativeValue& value) { return JS_NULL; } -} // namespace kraken::binding::qjs +} // namespace kraken diff --git a/bridge/foundation/native_value.h b/bridge/foundation/native_value.h index 705aa3c335..60f9ac51ae 100644 --- a/bridge/foundation/native_value.h +++ b/bridge/foundation/native_value.h @@ -67,6 +67,6 @@ NativeValue Native_NewJSON(ExecutionContext* context, JSValue& value); NativeValue jsValueToNativeValue(JSContext* ctx, JSValue& value); JSValue nativeValueToJSValue(ExecutionContext* context, NativeValue& value); -} // namespace kraken::binding::qjs +} // namespace kraken #endif // KRAKENBRIDGE_NATIVE_VALUE_H diff --git a/bridge/foundation/ref_ptr.h b/bridge/foundation/ref_ptr.h index ddadd0e382..3b47ebd881 100644 --- a/bridge/foundation/ref_ptr.h +++ b/bridge/foundation/ref_ptr.h @@ -13,8 +13,8 @@ #include #include "logging.h" -#include "ref_ptr_internal.h" #include "macros.h" +#include "ref_ptr_internal.h" namespace fml { diff --git a/bridge/foundation/ui_command_buffer.cc b/bridge/foundation/ui_command_buffer.cc index 25563796d5..b0046fa21b 100644 --- a/bridge/foundation/ui_command_buffer.cc +++ b/bridge/foundation/ui_command_buffer.cc @@ -9,7 +9,7 @@ namespace kraken { -UICommandBuffer::UICommandBuffer(ExecutionContext *context) : m_context(context) {} +UICommandBuffer::UICommandBuffer(ExecutionContext* context) : m_context(context) {} void UICommandBuffer::addCommand(int32_t id, int32_t type, void* nativePtr, bool batchedUpdate) { if (batchedUpdate) { diff --git a/bridge/foundation/ui_command_buffer.h b/bridge/foundation/ui_command_buffer.h index f59fd3e94f..033b7991ab 100644 --- a/bridge/foundation/ui_command_buffer.h +++ b/bridge/foundation/ui_command_buffer.h @@ -6,10 +6,10 @@ #ifndef KRAKENBRIDGE_FOUNDATION_UI_COMMAND_BUFFER_H_ #define KRAKENBRIDGE_FOUNDATION_UI_COMMAND_BUFFER_H_ -#include #include -#include "native_value.h" +#include #include "bindings/qjs/native_string_utils.h" +#include "native_value.h" namespace kraken { @@ -33,15 +33,15 @@ enum UICommand { struct UICommandItem { UICommandItem(int32_t id, int32_t type, NativeString args_01, NativeString args_02, void* nativePtr) - : type(type), - string_01(reinterpret_cast(args_01.string)), - args_01_length(args_01.length), - string_02(reinterpret_cast(args_02.string)), - args_02_length(args_02.length), - id(id), - nativePtr(reinterpret_cast(nativePtr)){}; + : type(type), + string_01(reinterpret_cast(args_01.string)), + args_01_length(args_01.length), + string_02(reinterpret_cast(args_02.string)), + args_02_length(args_02.length), + id(id), + nativePtr(reinterpret_cast(nativePtr)){}; UICommandItem(int32_t id, int32_t type, NativeString args_01, void* nativePtr) - : type(type), string_01(reinterpret_cast(args_01.string)), args_01_length(args_01.length), id(id), nativePtr(reinterpret_cast(nativePtr)){}; + : type(type), string_01(reinterpret_cast(args_01.string)), args_01_length(args_01.length), id(id), nativePtr(reinterpret_cast(nativePtr)){}; UICommandItem(int32_t id, int32_t type, void* nativePtr) : type(type), id(id), nativePtr(reinterpret_cast(nativePtr)){}; int32_t type; int32_t id; @@ -65,7 +65,7 @@ class UICommandBuffer { void clear(); private: - ExecutionContext *m_context{nullptr}; + ExecutionContext* m_context{nullptr}; std::atomic update_batched{false}; std::vector queue; }; diff --git a/bridge/kraken_bridge.cc b/bridge/kraken_bridge.cc index 3a3328c8d3..b6877af116 100644 --- a/bridge/kraken_bridge.cc +++ b/bridge/kraken_bridge.cc @@ -4,15 +4,15 @@ */ #include -#include #include +#include -#include "include/kraken_bridge.h" +#include "bindings/qjs/native_string_utils.h" #include "foundation/inspector_task_queue.h" #include "foundation/logging.h" -#include "foundation/ui_task_queue.h" #include "foundation/ui_command_buffer.h" -#include "bindings/qjs/native_string_utils.h" +#include "foundation/ui_task_queue.h" +#include "include/kraken_bridge.h" #include "page.h" #if defined(_WIN32) @@ -162,9 +162,9 @@ void registerDartMethods(int32_t contextId, uint64_t* methodBytes, int32_t lengt } NativeScreen* createScreen(double width, double height) { -// screen.width = width; -// screen.height = height; -// return &screen; + // screen.width = width; + // screen.height = height; + // return &screen; } static KrakenInfo* krakenInfo{nullptr}; diff --git a/bridge/kraken_bridge_test.cc b/bridge/kraken_bridge_test.cc index 46f5dcdfe6..7ff425d4fa 100644 --- a/bridge/kraken_bridge_test.cc +++ b/bridge/kraken_bridge_test.cc @@ -4,9 +4,9 @@ */ #include "kraken_bridge_test.h" -#include "page_test.h" -#include "bindings/qjs/native_string_utils.h" #include +#include "bindings/qjs/native_string_utils.h" +#include "page_test.h" std::unordered_map bridgeTestPool = std::unordered_map(); diff --git a/bridge/page.cc b/bridge/page.cc index 39525901e6..3ea6052bd5 100644 --- a/bridge/page.cc +++ b/bridge/page.cc @@ -7,11 +7,10 @@ #include #include -#include "foundation/logging.h" -#include "polyfill.h" #include "bindings/qjs/binding_initializer.h" +#include "foundation/logging.h" #include "page.h" - +#include "polyfill.h" namespace kraken { @@ -24,12 +23,15 @@ KrakenPage::KrakenPage(int32_t contextId, const JSExceptionHandler& handler) : c #if ENABLE_PROFILE auto jsContextStartTime = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); #endif - m_context = new ExecutionContext(contextId, [this](int32_t contextId, const char* message) { - if (m_context->dartMethodPtr()->onJsError != nullptr) { - m_context->dartMethodPtr()->onJsError(contextId, message); - } - KRAKEN_LOG(ERROR) << message << std::endl; - }, this); + m_context = new ExecutionContext( + contextId, + [this](int32_t contextId, const char* message) { + if (m_context->dartMethodPtr()->onJsError != nullptr) { + m_context->dartMethodPtr()->onJsError(contextId, message); + } + KRAKEN_LOG(ERROR) << message << std::endl; + }, + this); #if ENABLE_PROFILE auto nativePerformance = Performance::instance(m_context)->m_nativePerformance; @@ -57,56 +59,56 @@ KrakenPage::KrakenPage(int32_t contextId, const JSExceptionHandler& handler) : c } bool KrakenPage::parseHTML(const char* code, size_t length) { -// if (!m_context->isValid()) -// return false; -// JSValue bodyValue = JS_GetPropertyStr(m_context->ctx(), m_context->document()->jsObject, "body"); -// auto* body = static_cast(JS_GetOpaque(bodyValue, Element::classId)); -// HTMLParser::parseHTML(code, length, body); -// JS_FreeValue(m_context->ctx(), bodyValue); -// return true; + // if (!m_context->isValid()) + // return false; + // JSValue bodyValue = JS_GetPropertyStr(m_context->ctx(), m_context->document()->jsObject, "body"); + // auto* body = static_cast(JS_GetOpaque(bodyValue, Element::classId)); + // HTMLParser::parseHTML(code, length, body); + // JS_FreeValue(m_context->ctx(), bodyValue); + // return true; } void KrakenPage::invokeModuleEvent(const NativeString* moduleName, const char* eventType, void* ptr, NativeString* extra) { -// if (!m_context->isValid()) -// return; -// -// JSValue eventObject = JS_NULL; -// if (ptr != nullptr) { -// std::string type = std::string(eventType); -// auto* rawEvent = static_cast(ptr)->bytes; -// Event* event = Event::create(m_context->ctx(), reinterpret_cast(rawEvent)); -// eventObject = event->toQuickJS(); -// } -// -// JSValue moduleNameValue = JS_NewUnicodeString(m_context->runtime(), m_context->ctx(), moduleName->string, moduleName->length); -// JSValue extraObject = JS_NULL; -// if (extra != nullptr) { -// std::u16string u16Extra = std::u16string(reinterpret_cast(extra->string), extra->length); -// std::string extraString = toUTF8(u16Extra); -// extraObject = JS_ParseJSON(m_context->ctx(), extraString.c_str(), extraString.size(), ""); -// } -// -// { -// struct list_head *el, *el1; -// list_for_each_safe(el, el1, &m_context->module_job_list) { -// auto* module = list_entry(el, ModuleContext, link); -// JSValue callback = module->callback; -// -// JSValue arguments[] = {moduleNameValue, eventObject, extraObject}; -// JSValue returnValue = JS_Call(m_context->ctx(), callback, m_context->global(), 3, arguments); -// m_context->handleException(&returnValue); -// JS_FreeValue(m_context->ctx(), returnValue); -// } -// } -// -// JS_FreeValue(m_context->ctx(), moduleNameValue); -// -// if (rawEvent != nullptr) { -// JS_FreeValue(m_context->ctx(), eventObject); -// } -// if (extra != nullptr) { -// JS_FreeValue(m_context->ctx(), extraObject); -// } + // if (!m_context->isValid()) + // return; + // + // JSValue eventObject = JS_NULL; + // if (ptr != nullptr) { + // std::string type = std::string(eventType); + // auto* rawEvent = static_cast(ptr)->bytes; + // Event* event = Event::create(m_context->ctx(), reinterpret_cast(rawEvent)); + // eventObject = event->toQuickJS(); + // } + // + // JSValue moduleNameValue = JS_NewUnicodeString(m_context->runtime(), m_context->ctx(), moduleName->string, moduleName->length); + // JSValue extraObject = JS_NULL; + // if (extra != nullptr) { + // std::u16string u16Extra = std::u16string(reinterpret_cast(extra->string), extra->length); + // std::string extraString = toUTF8(u16Extra); + // extraObject = JS_ParseJSON(m_context->ctx(), extraString.c_str(), extraString.size(), ""); + // } + // + // { + // struct list_head *el, *el1; + // list_for_each_safe(el, el1, &m_context->module_job_list) { + // auto* module = list_entry(el, ModuleContext, link); + // JSValue callback = module->callback; + // + // JSValue arguments[] = {moduleNameValue, eventObject, extraObject}; + // JSValue returnValue = JS_Call(m_context->ctx(), callback, m_context->global(), 3, arguments); + // m_context->handleException(&returnValue); + // JS_FreeValue(m_context->ctx(), returnValue); + // } + // } + // + // JS_FreeValue(m_context->ctx(), moduleNameValue); + // + // if (rawEvent != nullptr) { + // JS_FreeValue(m_context->ctx(), eventObject); + // } + // if (extra != nullptr) { + // JS_FreeValue(m_context->ctx(), extraObject); + // } } void KrakenPage::evaluateScript(const NativeString* script, const char* url, int startLine) { diff --git a/bridge/page.h b/bridge/page.h index 9ced0129d1..de15d7856c 100644 --- a/bridge/page.h +++ b/bridge/page.h @@ -9,11 +9,11 @@ #include #include #include -#include #include +#include -#include "foundation/native_string.h" #include "core/executing_context.h" +#include "foundation/native_string.h" namespace kraken { diff --git a/bridge/page_test.cc b/bridge/page_test.cc index 61c6ed425a..b93e033937 100644 --- a/bridge/page_test.cc +++ b/bridge/page_test.cc @@ -39,63 +39,63 @@ static JSValue executeTest(JSContext* ctx, JSValueConst this_val, int argc, JSVa } static JSValue matchImageSnapshot(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) { -// JSValue& blobValue = argv[0]; -// JSValue& screenShotValue = argv[1]; -// JSValue& callbackValue = argv[2]; -// auto* context = static_cast(JS_GetContextOpaque(ctx)); -// -// if (!JS_IsObject(blobValue)) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 1 (blob) must be an Blob object."); -// } -// auto blob = static_cast(JS_GetOpaque(blobValue, kraken::Blob::kBlobClassID)); -// -// if (blob == nullptr) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 1 (blob) must be an Blob object."); -// } -// -// if (!JS_IsString(screenShotValue)) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 2 (match) must be an string."); -// } -// -// if (!JS_IsObject(callbackValue)) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 3 (callback) is not an function."); -// } -// -// if (!JS_IsFunction(ctx, callbackValue)) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 3 (callback) is not an function."); -// } -// -// if (getDartMethod()->matchImageSnapshot == nullptr) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': dart method (matchImageSnapshot) is not registered."); -// } -// -// std::unique_ptr screenShotNativeString = kraken::jsValueToNativeString(ctx, screenShotValue); -// auto bridge = static_cast(static_cast(context->getOwner())->owner); -// auto* callbackContext = new ImageSnapShotContext{JS_DupValue(ctx, callbackValue), context}; -// list_add_tail(&callbackContext->link, &bridge->image_link); -// -// auto fn = [](void* ptr, int32_t contextId, int8_t result, const char* errmsg) { -// auto* callbackContext = static_cast(ptr); -// JSContext* ctx = callbackContext->context->ctx(); -// -// if (errmsg == nullptr) { -// JSValue arguments[] = {JS_NewBool(ctx, result != 0), JS_NULL}; -// JSValue returnValue = JS_Call(ctx, callbackContext->callback, callbackContext->context->global(), 1, arguments); -// callbackContext->context->handleException(&returnValue); -// } else { -// JSValue errmsgValue = JS_NewString(ctx, errmsg); -// JSValue arguments[] = {JS_NewBool(ctx, false), errmsgValue}; -// JSValue returnValue = JS_Call(ctx, callbackContext->callback, callbackContext->context->global(), 2, arguments); -// callbackContext->context->handleException(&returnValue); -// JS_FreeValue(ctx, errmsgValue); -// } -// -// callbackContext->context->drainPendingPromiseJobs(); -// JS_FreeValue(callbackContext->context->ctx(), callbackContext->callback); -// list_del(&callbackContext->link); -// }; -// -// getDartMethod()->matchImageSnapshot(callbackContext, context->getContextId(), blob->bytes(), blob->size(), screenShotNativeString.get(), fn); + // JSValue& blobValue = argv[0]; + // JSValue& screenShotValue = argv[1]; + // JSValue& callbackValue = argv[2]; + // auto* context = static_cast(JS_GetContextOpaque(ctx)); + // + // if (!JS_IsObject(blobValue)) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 1 (blob) must be an Blob object."); + // } + // auto blob = static_cast(JS_GetOpaque(blobValue, kraken::Blob::kBlobClassID)); + // + // if (blob == nullptr) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 1 (blob) must be an Blob object."); + // } + // + // if (!JS_IsString(screenShotValue)) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 2 (match) must be an string."); + // } + // + // if (!JS_IsObject(callbackValue)) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 3 (callback) is not an function."); + // } + // + // if (!JS_IsFunction(ctx, callbackValue)) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': parameter 3 (callback) is not an function."); + // } + // + // if (getDartMethod()->matchImageSnapshot == nullptr) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_match_image_snapshot__': dart method (matchImageSnapshot) is not registered."); + // } + // + // std::unique_ptr screenShotNativeString = kraken::jsValueToNativeString(ctx, screenShotValue); + // auto bridge = static_cast(static_cast(context->getOwner())->owner); + // auto* callbackContext = new ImageSnapShotContext{JS_DupValue(ctx, callbackValue), context}; + // list_add_tail(&callbackContext->link, &bridge->image_link); + // + // auto fn = [](void* ptr, int32_t contextId, int8_t result, const char* errmsg) { + // auto* callbackContext = static_cast(ptr); + // JSContext* ctx = callbackContext->context->ctx(); + // + // if (errmsg == nullptr) { + // JSValue arguments[] = {JS_NewBool(ctx, result != 0), JS_NULL}; + // JSValue returnValue = JS_Call(ctx, callbackContext->callback, callbackContext->context->global(), 1, arguments); + // callbackContext->context->handleException(&returnValue); + // } else { + // JSValue errmsgValue = JS_NewString(ctx, errmsg); + // JSValue arguments[] = {JS_NewBool(ctx, false), errmsgValue}; + // JSValue returnValue = JS_Call(ctx, callbackContext->callback, callbackContext->context->global(), 2, arguments); + // callbackContext->context->handleException(&returnValue); + // JS_FreeValue(ctx, errmsgValue); + // } + // + // callbackContext->context->drainPendingPromiseJobs(); + // JS_FreeValue(callbackContext->context->ctx(), callbackContext->callback); + // list_del(&callbackContext->link); + // }; + // + // getDartMethod()->matchImageSnapshot(callbackContext, context->getContextId(), blob->bytes(), blob->size(), screenShotNativeString.get(), fn); return JS_NULL; } @@ -112,99 +112,99 @@ static JSValue environment(JSContext* ctx, JSValueConst this_val, int argc, JSVa } static JSValue simulatePointer(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) { -// if (getDartMethod()->simulatePointer == nullptr) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_pointer__': dart method(simulatePointer) is not registered."); -// } -// -// auto* context = static_cast(JS_GetContextOpaque(ctx)); -// -// JSValue inputArrayValue = argv[0]; -// if (!JS_IsObject(inputArrayValue)) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_pointer__': first arguments should be an array."); -// } -// -// JSValue pointerValue = argv[1]; -// if (!JS_IsNumber(pointerValue)) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_pointer__': second arguments should be an number."); -// } -// -// uint32_t length; -// JSValue lengthValue = JS_GetPropertyStr(ctx, inputArrayValue, "length"); -// JS_ToUint32(ctx, &length, lengthValue); -// JS_FreeValue(ctx, lengthValue); -// -// auto** mousePointerList = new MousePointer*[length]; -// -// for (int i = 0; i < length; i++) { -// auto mouse = new MousePointer(); -// JSValue params = JS_GetPropertyUint32(ctx, inputArrayValue, i); -// mouse->contextId = context->getContextId(); -// JSValue xValue = JS_GetPropertyUint32(ctx, params, 0); -// JSValue yValue = JS_GetPropertyUint32(ctx, params, 1); -// JSValue changeValue = JS_GetPropertyUint32(ctx, params, 2); -// -// double x; -// double y; -// double change; -// -// JS_ToFloat64(ctx, &x, xValue); -// JS_ToFloat64(ctx, &y, yValue); -// JS_ToFloat64(ctx, &change, changeValue); -// -// mouse->x = x; -// mouse->y = y; -// mouse->change = change; -// mousePointerList[i] = mouse; -// -// JS_FreeValue(ctx, params); -// JS_FreeValue(ctx, xValue); -// JS_FreeValue(ctx, yValue); -// JS_FreeValue(ctx, changeValue); -// } -// -// uint32_t pointer; -// JS_ToUint32(ctx, &pointer, pointerValue); -// -// getDartMethod()->simulatePointer(mousePointerList, length, pointer); -// -// delete[] mousePointerList; -// -// return JS_NULL; + // if (getDartMethod()->simulatePointer == nullptr) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_pointer__': dart method(simulatePointer) is not registered."); + // } + // + // auto* context = static_cast(JS_GetContextOpaque(ctx)); + // + // JSValue inputArrayValue = argv[0]; + // if (!JS_IsObject(inputArrayValue)) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_pointer__': first arguments should be an array."); + // } + // + // JSValue pointerValue = argv[1]; + // if (!JS_IsNumber(pointerValue)) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_pointer__': second arguments should be an number."); + // } + // + // uint32_t length; + // JSValue lengthValue = JS_GetPropertyStr(ctx, inputArrayValue, "length"); + // JS_ToUint32(ctx, &length, lengthValue); + // JS_FreeValue(ctx, lengthValue); + // + // auto** mousePointerList = new MousePointer*[length]; + // + // for (int i = 0; i < length; i++) { + // auto mouse = new MousePointer(); + // JSValue params = JS_GetPropertyUint32(ctx, inputArrayValue, i); + // mouse->contextId = context->getContextId(); + // JSValue xValue = JS_GetPropertyUint32(ctx, params, 0); + // JSValue yValue = JS_GetPropertyUint32(ctx, params, 1); + // JSValue changeValue = JS_GetPropertyUint32(ctx, params, 2); + // + // double x; + // double y; + // double change; + // + // JS_ToFloat64(ctx, &x, xValue); + // JS_ToFloat64(ctx, &y, yValue); + // JS_ToFloat64(ctx, &change, changeValue); + // + // mouse->x = x; + // mouse->y = y; + // mouse->change = change; + // mousePointerList[i] = mouse; + // + // JS_FreeValue(ctx, params); + // JS_FreeValue(ctx, xValue); + // JS_FreeValue(ctx, yValue); + // JS_FreeValue(ctx, changeValue); + // } + // + // uint32_t pointer; + // JS_ToUint32(ctx, &pointer, pointerValue); + // + // getDartMethod()->simulatePointer(mousePointerList, length, pointer); + // + // delete[] mousePointerList; + // + // return JS_NULL; } -static JSValue simulateInputText(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) { -// if (getDartMethod()->simulateInputText == nullptr) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_keypress__': dart method(simulateInputText) is not registered."); -// } -// -// JSValue& charStringValue = argv[0]; -// -// if (!JS_IsString(charStringValue)) { -// return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_keypress__': first arguments should be a string"); -// } -// -// std::unique_ptr nativeString = kraken::jsValueToNativeString(ctx, charStringValue); -// getDartMethod()->simulateInputText(nativeString.get()); -// nativeString->free(); -// return JS_NULL; +static JSValue simulateInputText(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv){ + // if (getDartMethod()->simulateInputText == nullptr) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_keypress__': dart method(simulateInputText) is not registered."); + // } + // + // JSValue& charStringValue = argv[0]; + // + // if (!JS_IsString(charStringValue)) { + // return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_simulate_keypress__': first arguments should be a string"); + // } + // + // std::unique_ptr nativeString = kraken::jsValueToNativeString(ctx, charStringValue); + // getDartMethod()->simulateInputText(nativeString.get()); + // nativeString->free(); + // return JS_NULL; }; static JSValue parseHTML(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) { -// auto* context = static_cast(JS_GetContextOpaque(ctx)); -// -// if (argc == 1) { -// JSValue& html = argv[0]; -// -// std::string strHTML = jsValueToStdString(ctx, html); -// -// JSValue bodyValue = JS_GetPropertyStr(context->ctx(), context->document()->jsObject, "body"); -// auto* body = static_cast(JS_GetOpaque(bodyValue, Element::classId())); -// HTMLParser::parseHTML(strHTML, body); -// -// JS_FreeValue(ctx, bodyValue); -// } -// -// return JS_NULL; + // auto* context = static_cast(JS_GetContextOpaque(ctx)); + // + // if (argc == 1) { + // JSValue& html = argv[0]; + // + // std::string strHTML = jsValueToStdString(ctx, html); + // + // JSValue bodyValue = JS_GetPropertyStr(context->ctx(), context->document()->jsObject, "body"); + // auto* body = static_cast(JS_GetOpaque(bodyValue, Element::classId())); + // HTMLParser::parseHTML(strHTML, body); + // + // JS_FreeValue(ctx, bodyValue); + // } + // + // return JS_NULL; } static JSValue triggerGlobalError(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) { @@ -222,18 +222,18 @@ static JSValue triggerGlobalError(JSContext* ctx, JSValueConst this_val, int arg } KrakenPageTest::KrakenPageTest(KrakenPage* bridge) : m_page(bridge), m_page_context(bridge->getContext()) { -// bridge->owner = this; -// bridge->disposeCallback = [](KrakenPage* bridge) { delete static_cast(bridge->owner); }; -// QJS_GLOBAL_BINDING_FUNCTION(m_page_context, executeTest, "__kraken_execute_test__", 1); -// QJS_GLOBAL_BINDING_FUNCTION(m_page_context, matchImageSnapshot, "__kraken_match_image_snapshot__", 3); -// QJS_GLOBAL_BINDING_FUNCTION(m_page_context, environment, "__kraken_environment__", 0); -// QJS_GLOBAL_BINDING_FUNCTION(m_page_context, simulatePointer, "__kraken_simulate_pointer__", 1); -// QJS_GLOBAL_BINDING_FUNCTION(m_page_context, simulateInputText, "__kraken_simulate_inputtext__", 1); -// QJS_GLOBAL_BINDING_FUNCTION(m_page_context, triggerGlobalError, "__kraken_trigger_global_error__", 0); -// QJS_GLOBAL_BINDING_FUNCTION(m_page_context, parseHTML, "__kraken_parse_html__", 1); + // bridge->owner = this; + // bridge->disposeCallback = [](KrakenPage* bridge) { delete static_cast(bridge->owner); }; + // QJS_GLOBAL_BINDING_FUNCTION(m_page_context, executeTest, "__kraken_execute_test__", 1); + // QJS_GLOBAL_BINDING_FUNCTION(m_page_context, matchImageSnapshot, "__kraken_match_image_snapshot__", 3); + // QJS_GLOBAL_BINDING_FUNCTION(m_page_context, environment, "__kraken_environment__", 0); + // QJS_GLOBAL_BINDING_FUNCTION(m_page_context, simulatePointer, "__kraken_simulate_pointer__", 1); + // QJS_GLOBAL_BINDING_FUNCTION(m_page_context, simulateInputText, "__kraken_simulate_inputtext__", 1); + // QJS_GLOBAL_BINDING_FUNCTION(m_page_context, triggerGlobalError, "__kraken_trigger_global_error__", 0); + // QJS_GLOBAL_BINDING_FUNCTION(m_page_context, parseHTML, "__kraken_parse_html__", 1); -// initKrakenTestFramework(bridge); -// init_list_head(&image_link); + // initKrakenTestFramework(bridge); + // init_list_head(&image_link); } struct ExecuteCallbackContext { @@ -245,39 +245,39 @@ struct ExecuteCallbackContext { }; void KrakenPageTest::invokeExecuteTest(ExecuteCallback executeCallback) { -// if (JS_IsNull(executeTestCallback)) { -// return; -// } -// if (!JS_IsFunction(m_page_context->ctx(), executeTestCallback)) { -// return; -// } -// -// auto done = [](JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* func_data) -> JSValue { -// JSValue& statusValue = argv[0]; -// JSValue proxyObject = func_data[0]; -// auto* callbackContext = static_cast(JS_GetOpaque(proxyObject, 1)); -// -// if (!JS_IsString(statusValue)) { -// return JS_ThrowTypeError(ctx, "failed to execute 'done': parameter 1 (status) is not a string"); -// } -// -// std::unique_ptr status = kraken::jsValueToNativeString(ctx, statusValue); -// callbackContext->executeCallback(callbackContext->context->getContextId(), status.get()); -// return JS_NULL; -// }; -// auto* callbackContext = new ExecuteCallbackContext(m_page_context, executeCallback); -// executeTestProxyObject = JS_NewObject(m_page_context->ctx()); -// JS_SetOpaque(executeTestProxyObject, callbackContext); -// JSValue callbackData[]{executeTestProxyObject}; -// JSValue callback = JS_NewCFunctionData(m_page_context->ctx(), done, 0, 0, 1, callbackData); -// -// JSValue arguments[] = {callback}; -// JSValue result = JS_Call(m_page_context->ctx(), executeTestCallback, executeTestCallback, 1, arguments); -// m_page_context->handleException(&result); -// m_page_context->drainPendingPromiseJobs(); -// JS_FreeValue(m_page_context->ctx(), executeTestCallback); -// JS_FreeValue(m_page_context->ctx(), callback); -// executeTestCallback = JS_NULL; + // if (JS_IsNull(executeTestCallback)) { + // return; + // } + // if (!JS_IsFunction(m_page_context->ctx(), executeTestCallback)) { + // return; + // } + // + // auto done = [](JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* func_data) -> JSValue { + // JSValue& statusValue = argv[0]; + // JSValue proxyObject = func_data[0]; + // auto* callbackContext = static_cast(JS_GetOpaque(proxyObject, 1)); + // + // if (!JS_IsString(statusValue)) { + // return JS_ThrowTypeError(ctx, "failed to execute 'done': parameter 1 (status) is not a string"); + // } + // + // std::unique_ptr status = kraken::jsValueToNativeString(ctx, statusValue); + // callbackContext->executeCallback(callbackContext->context->getContextId(), status.get()); + // return JS_NULL; + // }; + // auto* callbackContext = new ExecuteCallbackContext(m_page_context, executeCallback); + // executeTestProxyObject = JS_NewObject(m_page_context->ctx()); + // JS_SetOpaque(executeTestProxyObject, callbackContext); + // JSValue callbackData[]{executeTestProxyObject}; + // JSValue callback = JS_NewCFunctionData(m_page_context->ctx(), done, 0, 0, 1, callbackData); + // + // JSValue arguments[] = {callback}; + // JSValue result = JS_Call(m_page_context->ctx(), executeTestCallback, executeTestCallback, 1, arguments); + // m_page_context->handleException(&result); + // m_page_context->drainPendingPromiseJobs(); + // JS_FreeValue(m_page_context->ctx(), executeTestCallback); + // JS_FreeValue(m_page_context->ctx(), callback); + // executeTestCallback = JS_NULL; } void KrakenPageTest::registerTestEnvDartMethods(uint64_t* methodBytes, int32_t length) { diff --git a/bridge/page_test.h b/bridge/page_test.h index 59a6f4aa06..219e4d060d 100644 --- a/bridge/page_test.h +++ b/bridge/page_test.h @@ -6,8 +6,8 @@ #ifndef KRAKENBRIDGE_PAGE_TEST_H #define KRAKENBRIDGE_PAGE_TEST_H -#include "kraken_bridge_test.h" #include "core/executing_context.h" +#include "kraken_bridge_test.h" #include "page.h" namespace kraken { diff --git a/bridge/test/kraken_test_env.cc b/bridge/test/kraken_test_env.cc index 3584813b1e..84df6a5435 100644 --- a/bridge/test/kraken_test_env.cc +++ b/bridge/test/kraken_test_env.cc @@ -12,21 +12,21 @@ //#include "page.h" // //#if defined(__linux__) || defined(__APPLE__) -//static int64_t get_time_ms(void) { +// static int64_t get_time_ms(void) { // struct timespec ts; // clock_gettime(CLOCK_MONOTONIC, &ts); // return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000); //} //#else ///* more portable, but does not work if the date is updated */ -//static int64_t get_time_ms(void) { +// static int64_t get_time_ms(void) { // struct timeval tv; // gettimeofday(&tv, NULL); // return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000); //} //#endif // -//typedef struct { +// typedef struct { // struct list_head link; // int64_t timeout; // DOMTimer* timer; @@ -35,7 +35,7 @@ // AsyncCallback func; //} JSOSTimer; // -//typedef struct { +// typedef struct { // struct list_head link; // FrameCallback* callback; // int32_t contextId; @@ -43,30 +43,30 @@ // int32_t callbackId; //} JSFrameCallback; // -//typedef struct JSThreadState { +// typedef struct JSThreadState { // std::unordered_map os_timers; /* list of timer.link */ // std::unordered_map os_frameCallbacks; //} JSThreadState; // -//static void unlink_timer(JSThreadState* ts, JSOSTimer* th) { +// static void unlink_timer(JSThreadState* ts, JSOSTimer* th) { // ts->os_timers.erase(th->timer->timerId()); //} // -//static void unlink_callback(JSThreadState* ts, JSFrameCallback* th) { +// static void unlink_callback(JSThreadState* ts, JSFrameCallback* th) { // ts->os_frameCallbacks.erase(th->callbackId); //} // -//NativeString* TEST_invokeModule(void* callbackContext, int32_t contextId, NativeString* moduleName, NativeString* method, NativeString* params, AsyncModuleCallback callback) { +// NativeString* TEST_invokeModule(void* callbackContext, int32_t contextId, NativeString* moduleName, NativeString* method, NativeString* params, AsyncModuleCallback callback) { // return nullptr; //}; // -//void TEST_requestBatchUpdate(int32_t contextId){}; +// void TEST_requestBatchUpdate(int32_t contextId){}; // -//void TEST_reloadApp(int32_t contextId) {} +// void TEST_reloadApp(int32_t contextId) {} // -//int32_t timerId = 0; +// int32_t timerId = 0; // -//int32_t TEST_setTimeout(DOMTimer* timer, int32_t contextId, AsyncCallback callback, int32_t timeout) { +// int32_t TEST_setTimeout(DOMTimer* timer, int32_t contextId, AsyncCallback callback, int32_t timeout) { // JSRuntime* rt = JS_GetRuntime(timer->ctx()); // auto* context = static_cast(JS_GetContextOpaque(timer->ctx())); // JSThreadState* ts = static_cast(JS_GetRuntimeOpaque(rt)); @@ -83,7 +83,7 @@ // return id; //} // -//int32_t TEST_setInterval(DOMTimer* timer, int32_t contextId, AsyncCallback callback, int32_t timeout) { +// int32_t TEST_setInterval(DOMTimer* timer, int32_t contextId, AsyncCallback callback, int32_t timeout) { // JSRuntime* rt = JS_GetRuntime(timer->ctx()); // auto* context = static_cast(JS_GetContextOpaque(timer->ctx())); // JSThreadState* ts = static_cast(JS_GetRuntimeOpaque(rt)); @@ -100,9 +100,9 @@ // return id; //} // -//int32_t callbackId = 0; +// int32_t callbackId = 0; // -//uint32_t TEST_requestAnimationFrame(FrameCallback* frameCallback, int32_t contextId, AsyncRAFCallback handler) { +// uint32_t TEST_requestAnimationFrame(FrameCallback* frameCallback, int32_t contextId, AsyncRAFCallback handler) { // JSRuntime* rt = JS_GetRuntime(frameCallback->ctx()); // auto* context = static_cast(JS_GetContextOpaque(frameCallback->ctx())); // JSThreadState* ts = static_cast(JS_GetRuntimeOpaque(rt)); @@ -119,48 +119,48 @@ // return id; //} // -//void TEST_cancelAnimationFrame(int32_t contextId, int32_t id) { +// void TEST_cancelAnimationFrame(int32_t contextId, int32_t id) { // auto* page = static_cast(getPage(contextId)); // auto* context = page->getContext(); // JSThreadState* ts = static_cast(JS_GetRuntimeOpaque(context->runtime())); // ts->os_frameCallbacks.erase(id); //} // -//void TEST_clearTimeout(int32_t contextId, int32_t timerId) { +// void TEST_clearTimeout(int32_t contextId, int32_t timerId) { // auto* page = static_cast(getPage(contextId)); // auto* context = page->getContext(); // JSThreadState* ts = static_cast(JS_GetRuntimeOpaque(context->runtime())); // ts->os_timers.erase(timerId); //} // -//NativeScreen* TEST_getScreen(int32_t contextId) { +// NativeScreen* TEST_getScreen(int32_t contextId) { // return nullptr; //}; // -//double TEST_devicePixelRatio(int32_t contextId) { +// double TEST_devicePixelRatio(int32_t contextId) { // return 1.0; //} // -//NativeString* TEST_platformBrightness(int32_t contextId) { +// NativeString* TEST_platformBrightness(int32_t contextId) { // return nullptr; //} // -//void TEST_toBlob(void* callbackContext, int32_t contextId, AsyncBlobCallback blobCallback, int32_t elementId, double devicePixelRatio) {} +// void TEST_toBlob(void* callbackContext, int32_t contextId, AsyncBlobCallback blobCallback, int32_t elementId, double devicePixelRatio) {} // -//void TEST_flushUICommand() {} +// void TEST_flushUICommand() {} // -//void TEST_initWindow(int32_t contextId, void* nativePtr) {} +// void TEST_initWindow(int32_t contextId, void* nativePtr) {} // -//void TEST_initDocument(int32_t contextId, void* nativePtr) {} +// void TEST_initDocument(int32_t contextId, void* nativePtr) {} // //#if ENABLE_PROFILE -//NativePerformanceEntryList* TEST_getPerformanceEntries(int32_t) {} +// NativePerformanceEntryList* TEST_getPerformanceEntries(int32_t) {} //#endif // -//std::once_flag testInitOnceFlag; -//static int32_t inited{false}; +// std::once_flag testInitOnceFlag; +// static int32_t inited{false}; // -//std::unique_ptr TEST_init(OnJSError onJsError) { +// std::unique_ptr TEST_init(OnJSError onJsError) { // uint32_t contextId; // if (inited) { // contextId = allocateNewPage(-1); @@ -182,17 +182,17 @@ // return std::unique_ptr(page); //} // -//std::unique_ptr TEST_init() { +// std::unique_ptr TEST_init() { // return TEST_init(nullptr); //} // -//std::unique_ptr TEST_allocateNewPage() { +// std::unique_ptr TEST_allocateNewPage() { // uint32_t newContextId = allocateNewPage(-1); // initTestFramework(newContextId); // return std::unique_ptr(static_cast(getPage(newContextId))); //} // -//static bool jsPool(ExecutionContext* context) { +// static bool jsPool(ExecutionContext* context) { // JSRuntime* rt = context->runtime(); // JSThreadState* ts = static_cast(JS_GetRuntimeOpaque(rt)); // int64_t cur_time, delay; @@ -238,7 +238,7 @@ // return false; //} // -//void TEST_runLoop(ExecutionContext* context) { +// void TEST_runLoop(ExecutionContext* context) { // for (;;) { // context->drainPendingPromiseJobs(); // if (jsPool(context)) @@ -246,7 +246,7 @@ // } //} // -//void TEST_dispatchEvent(int32_t contextId, EventTarget* eventTarget, const std::string type) { +// void TEST_dispatchEvent(int32_t contextId, EventTarget* eventTarget, const std::string type) { // NativeEventTarget* nativeEventTarget = new NativeEventTarget(eventTarget); // auto nativeEventType = stringToNativeString(type); // NativeString* rawEventType = nativeEventType.release(); @@ -258,10 +258,10 @@ // NativeEventTarget::dispatchEventImpl(contextId, nativeEventTarget, rawEventType, rawEvent, false); //} // -//void TEST_callNativeMethod(void* nativePtr, void* returnValue, void* method, int32_t argc, void* argv) {} +// void TEST_callNativeMethod(void* nativePtr, void* returnValue, void* method, int32_t argc, void* argv) {} // -//std::unordered_map> unitTestEnvMap; -//std::shared_ptr TEST_getEnv(int32_t contextUniqueId) { +// std::unordered_map> unitTestEnvMap; +// std::shared_ptr TEST_getEnv(int32_t contextUniqueId) { // if (unitTestEnvMap.count(contextUniqueId) == 0) { // unitTestEnvMap[contextUniqueId] = std::make_shared(); // } @@ -269,7 +269,7 @@ // return unitTestEnvMap[contextUniqueId]; //} // -//void TEST_registerEventTargetDisposedCallback(int32_t contextUniqueId, TEST_OnEventTargetDisposed callback) { +// void TEST_registerEventTargetDisposedCallback(int32_t contextUniqueId, TEST_OnEventTargetDisposed callback) { // if (unitTestEnvMap.count(contextUniqueId) == 0) { // unitTestEnvMap[contextUniqueId] = std::make_shared(); // } @@ -277,7 +277,7 @@ // unitTestEnvMap[contextUniqueId]->onEventTargetDisposed = callback; //} // -//void TEST_mockDartMethods(OnJSError onJSError) { +// void TEST_mockDartMethods(OnJSError onJSError) { // std::vector mockMethods{ // reinterpret_cast(TEST_invokeModule), // reinterpret_cast(TEST_requestBatchUpdate), diff --git a/bridge/test/kraken_test_env.h b/bridge/test/kraken_test_env.h index 8078fd3a2b..bb3138e662 100644 --- a/bridge/test/kraken_test_env.h +++ b/bridge/test/kraken_test_env.h @@ -13,21 +13,21 @@ #include "page.h" // //// Trigger a callbacks before GC free the eventTargets. -//using TEST_OnEventTargetDisposed = void (*)(kraken::binding::qjs::EventTargetInstance* eventTargetInstance); -//struct UnitTestEnv { +// using TEST_OnEventTargetDisposed = void (*)(kraken::binding::qjs::EventTargetInstance* eventTargetInstance); +// struct UnitTestEnv { // TEST_OnEventTargetDisposed onEventTargetDisposed{nullptr}; //}; // //// Mock dart methods and add async timer to emulate kraken environment in C++ unit test. // -//std::unique_ptr TEST_init(OnJSError onJsError); -//std::unique_ptr TEST_init(); -//std::unique_ptr TEST_allocateNewPage(); -//void TEST_runLoop(ExecutionContext* context); -//void TEST_dispatchEvent(int32_t contextId, EventTarget* eventTarget, const std::string type); -//void TEST_callNativeMethod(void* nativePtr, void* returnValue, void* method, int32_t argc, void* argv); -//void TEST_registerEventTargetDisposedCallback(int32_t contextUniqueId, TEST_OnEventTargetDisposed callback); -//void TEST_mockDartMethods(OnJSError onJSError); -//std::shared_ptr TEST_getEnv(int32_t contextUniqueId); +// std::unique_ptr TEST_init(OnJSError onJsError); +// std::unique_ptr TEST_init(); +// std::unique_ptr TEST_allocateNewPage(); +// void TEST_runLoop(ExecutionContext* context); +// void TEST_dispatchEvent(int32_t contextId, EventTarget* eventTarget, const std::string type); +// void TEST_callNativeMethod(void* nativePtr, void* returnValue, void* method, int32_t argc, void* argv); +// void TEST_registerEventTargetDisposedCallback(int32_t contextUniqueId, TEST_OnEventTargetDisposed callback); +// void TEST_mockDartMethods(OnJSError onJSError); +// std::shared_ptr TEST_getEnv(int32_t contextUniqueId); #endif // KRAKENBRIDGE_TEST_KRAKEN_TEST_ENV_H_