diff --git a/docs/knowledge/v8-14-migration.md b/docs/knowledge/v8-14-migration.md index 24c0d26db..8256e32d5 100644 --- a/docs/knowledge/v8-14-migration.md +++ b/docs/knowledge/v8-14-migration.md @@ -34,7 +34,7 @@ new, and why: **`v8_array_buffer_view_internal_field_count=2`** — both default to `0` in 14.9 and defaulted to `2` in 10.3. When an `ArrayBuffer`/`SharedArrayBuffer`/ typed array is marshalled to a Java NIO buffer, - `JSToJavaObjectsConverter` calls `ObjectManager::Link` on the buffer object + `JsArgConverter` calls `ObjectManager::Link` on the buffer object itself, and `Link` stores its `JSInstanceInfo` in internal field 0. With zero internal fields every such conversion throws *"Trying to link invalid 'this' to a Java object"*. `v8-array-buffer.h` still falls back to `2` when the macro diff --git a/test-app/runtime/CMakeLists.txt b/test-app/runtime/CMakeLists.txt index f638b92a1..1ceb096ae 100644 --- a/test-app/runtime/CMakeLists.txt +++ b/test-app/runtime/CMakeLists.txt @@ -230,10 +230,6 @@ add_library( src/main/cpp/com_tns_AssetExtractor.cpp src/main/cpp/com_tns_Runtime.cpp src/main/cpp/console/Console.cpp - src/main/cpp/conversions/JSToJavaConverter.cpp - src/main/cpp/conversions/objects/JSToJavaObjectsConverter.cpp - src/main/cpp/conversions/arrays/JSToJavaArraysConverter.cpp - src/main/cpp/conversions/primitives/JSToJavaPrimitivesConverter.cpp src/main/cpp/ada/ada.cpp src/main/cpp/URLImpl.cpp src/main/cpp/URLSearchParamsImpl.cpp diff --git a/test-app/runtime/src/main/cpp/CallbackHandlers.cpp b/test-app/runtime/src/main/cpp/CallbackHandlers.cpp index 8d2b18135..5f56d01c5 100644 --- a/test-app/runtime/src/main/cpp/CallbackHandlers.cpp +++ b/test-app/runtime/src/main/cpp/CallbackHandlers.cpp @@ -5,7 +5,6 @@ #include "V8GlobalHelpers.h" #include "V8StringConstants.h" #include "Constants.h" -//#include "./conversions/JSToJavaConverter.h" #include "JsArgConverter.h" #include "JsArgToArrayConverter.h" #include "ArgConverter.h" @@ -140,7 +139,6 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local &j instance = env.NewObject(generatedJavaClass, mi.mid); } else { // resolve arguments before passing them on to the constructor -// JSToJavaConverter argConverter(isolate, argWrapper.args, mi.signature); JsArgConverter argConverter(argWrapper.args, mi.signature); auto ctorArgs = argConverter.ToArgs(); @@ -366,10 +364,6 @@ void CallbackHandlers::CallJavaMethod(const Local &caller, const string methodName.c_str()); } -// JSToJavaConverter argConverter = (entry != nullptr && entry->isExtensionFunction) -// ? JSToJavaConverter(isolate, args, *sig, entry, caller) -// : JSToJavaConverter(isolate, args, *sig, entry); - JsArgConverter argConverter = (entry != nullptr && entry->isExtensionFunction) ? JsArgConverter(caller, args, *sig, entry) : JsArgConverter(args, false, *sig, entry); diff --git a/test-app/runtime/src/main/cpp/JsArgConverter.cpp b/test-app/runtime/src/main/cpp/JsArgConverter.cpp index e5d82f0bd..bda086671 100644 --- a/test-app/runtime/src/main/cpp/JsArgConverter.cpp +++ b/test-app/runtime/src/main/cpp/JsArgConverter.cpp @@ -28,10 +28,10 @@ JsArgConverter::JsArgConverter(const Local &caller, JniSignatureParser parser(m_methodSignature); entry->parsedSig = parser.Parse(); } - m_tokens = entry->parsedSig; + m_tokens = &entry->parsedSig; } else { JniSignatureParser parser(m_methodSignature); - m_tokens = parser.Parse(); + m_ownedTokens = parser.Parse(); } m_isValid = ConvertArg(caller, 0); @@ -63,10 +63,10 @@ JsArgConverter::JsArgConverter(const v8::FunctionCallbackInfo &args, JniSignatureParser parser(m_methodSignature); entry->parsedSig = parser.Parse(); } - m_tokens = entry->parsedSig; + m_tokens = &entry->parsedSig; } else { JniSignatureParser parser(m_methodSignature); - m_tokens = parser.Parse(); + m_ownedTokens = parser.Parse(); } for (int i = 0; i < m_argsLen; i++) { @@ -86,7 +86,7 @@ JsArgConverter::JsArgConverter(const v8::FunctionCallbackInfo &args, m_argsLen = args.Length(); JniSignatureParser parser(m_methodSignature); - m_tokens = parser.Parse(); + m_ownedTokens = parser.Parse(); for (int i = 0; i < m_argsLen; i++) { m_isValid = ConvertArg(args[i], i); @@ -117,7 +117,7 @@ bool JsArgConverter::ConvertArg(const Local &arg, int index) { char buff[1024]; - const auto &typeSignature = m_tokens.at(index); + const auto &typeSignature = m_tokens->at(index); if (arg.IsEmpty()) { SetConvertedObject(index, nullptr); @@ -394,7 +394,7 @@ bool JsArgConverter::ConvertJavaScriptNumber(const Local &jsValue, int in 0 }; - const auto &typeSignature = m_tokens.at(index); + const auto &typeSignature = m_tokens->at(index); auto context = m_isolate->GetCurrentContext(); const char typePrefix = typeSignature[0]; @@ -445,7 +445,7 @@ bool JsArgConverter::ConvertJavaScriptNumber(const Local &jsValue, int in bool JsArgConverter::ConvertJavaScriptBoolean(const Local &jsValue, int index) { bool success; - const auto &typeSignature = m_tokens.at(index); + const auto &typeSignature = m_tokens->at(index); auto context = m_isolate->GetCurrentContext(); if (typeSignature == "Z") { @@ -489,7 +489,7 @@ bool JsArgConverter::ConvertJavaScriptArray(const Local &jsArr, int index jsize arrLength = jsArr->Length(); - const auto &arraySignature = m_tokens.at(index); + const auto &arraySignature = m_tokens->at(index); auto context = m_isolate->GetCurrentContext(); string elementType = arraySignature.substr(1); @@ -632,7 +632,7 @@ template bool JsArgConverter::ConvertFromCastFunctionObject(T value, int index) { bool success = false; - const auto &typeSignature = m_tokens.at(index); + const auto &typeSignature = m_tokens->at(index); const char typeSignaturePrefix = typeSignature[0]; diff --git a/test-app/runtime/src/main/cpp/JsArgConverter.h b/test-app/runtime/src/main/cpp/JsArgConverter.h index ba4678d88..39930ea41 100644 --- a/test-app/runtime/src/main/cpp/JsArgConverter.h +++ b/test-app/runtime/src/main/cpp/JsArgConverter.h @@ -27,6 +27,10 @@ namespace tns { JsArgConverter(const v8::FunctionCallbackInfo& args, const std::string& methodSignature); + JsArgConverter(const JsArgConverter&) = delete; + + JsArgConverter& operator=(const JsArgConverter&) = delete; + ~JsArgConverter(); jvalue* ToArgs(); @@ -79,7 +83,10 @@ namespace tns { std::string m_methodSignature; - std::vector m_tokens; + std::vector m_ownedTokens; + + // May point at m_ownedTokens, which is why the converter is not copyable. + const std::vector* m_tokens = &m_ownedTokens; Error m_error; }; diff --git a/test-app/runtime/src/main/cpp/conversions/CastTypes.h b/test-app/runtime/src/main/cpp/conversions/CastTypes.h deleted file mode 100644 index f0fd6a616..000000000 --- a/test-app/runtime/src/main/cpp/conversions/CastTypes.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef TEST_APP_CASTTYPES_H -#define TEST_APP_CASTTYPES_H - -#include "v8.h" - -namespace tns { - - enum class JavaObjectCastType { - Byte, - Short, - Int, - Long, - Float, - Double - }; - - tns::JavaObjectCastType GetCastType(const v8::Local &view) { - if (view->IsUint16Array() || view->IsInt16Array()) { - return tns::JavaObjectCastType::Short; - } else if (view->IsUint32Array() || view->IsInt32Array()) { - return tns::JavaObjectCastType::Int; - } else if (view->IsFloat32Array()) { - return tns::JavaObjectCastType::Float; - } else if (view->IsFloat64Array()) { - return tns::JavaObjectCastType::Double; - } else if (view->IsBigUint64Array() || view->IsBigInt64Array()) { - return tns::JavaObjectCastType::Long; - } - return tns::JavaObjectCastType::Byte; - } - -} - -#endif //TEST_APP_CASTTYPES_H diff --git a/test-app/runtime/src/main/cpp/conversions/ConversionConsts.h b/test-app/runtime/src/main/cpp/conversions/ConversionConsts.h deleted file mode 100644 index a970cc049..000000000 --- a/test-app/runtime/src/main/cpp/conversions/ConversionConsts.h +++ /dev/null @@ -1,10 +0,0 @@ -// -// Created by Vladimir Mutafov on 16.11.22. -// - -#ifndef TEST_APP_CONVERSIONCONSTS_H -#define TEST_APP_CONVERSIONCONSTS_H - -#define JAVA_METHOD_MAX_ARGS_LENGTH 255 - -#endif //TEST_APP_CONVERSIONCONSTS_H diff --git a/test-app/runtime/src/main/cpp/conversions/JNIArgRefsState.h b/test-app/runtime/src/main/cpp/conversions/JNIArgRefsState.h deleted file mode 100644 index 7d9f17495..000000000 --- a/test-app/runtime/src/main/cpp/conversions/JNIArgRefsState.h +++ /dev/null @@ -1,26 +0,0 @@ -// -// Created by Vladimir Mutafov on 16.11.22. -// - -#ifndef TEST_APP_JNIARGREFSSTATE_H -#define TEST_APP_JNIARGREFSSTATE_H - -#include "JEnv.h" -#include "ConversionConsts.h" - -namespace tns { - struct JNIArgRefsState { - jvalue m_args[JAVA_METHOD_MAX_ARGS_LENGTH]; - int m_args_refs[JAVA_METHOD_MAX_ARGS_LENGTH]; - int m_args_refs_size = 0; - - void SetConvertedObject(int index, jobject obj, bool isGlobal = false) { - m_args[index].l = obj; - if ((obj != nullptr) && !isGlobal) { - m_args_refs[m_args_refs_size++] = index; - } - } - }; -} - -#endif //TEST_APP_JNIARGREFSSTATE_H diff --git a/test-app/runtime/src/main/cpp/conversions/JSToJavaConverter.cpp b/test-app/runtime/src/main/cpp/conversions/JSToJavaConverter.cpp deleted file mode 100644 index ac9cefc5e..000000000 --- a/test-app/runtime/src/main/cpp/conversions/JSToJavaConverter.cpp +++ /dev/null @@ -1,189 +0,0 @@ -#include "JSToJavaConverter.h" -#include "ObjectManager.h" -#include "JniSignatureParser.h" -#include "JsArgToArrayConverter.h" -#include "ArgConverter.h" -#include "V8StringConstants.h" -#include "NumericCasts.h" -#include "NativeScriptException.h" -#include "Runtime.h" -#include "V8GlobalHelpers.h" -#include -#include "CastTypes.h" -#include "./objects/JSToJavaObjectsConverter.h" -#include "./arrays/JSToJavaArraysConverter.h" -#include "./primitives/JSToJavaPrimitivesConverter.h" - -using namespace v8; -using namespace std; -using namespace tns; - -JSToJavaConverter::JSToJavaConverter( - v8::Isolate* isolate, - const FunctionCallbackInfo &args, - const string &methodSignature, - MetadataEntry *entry) - : - m_isolate(isolate), - m_methodSignature(methodSignature) { - m_argsLen = args.Length(); - - if (m_argsLen > 0) { - if ((entry != nullptr) && (entry->isResolved)) { - if (entry->parsedSig.empty()) { - JniSignatureParser parser(m_methodSignature); - entry->parsedSig = parser.Parse(); - } - m_tokens = &entry->parsedSig; - } else { - JniSignatureParser parser(m_methodSignature); - std::vector parsed = parser.Parse(); - m_tokens = new std::vector(); - m_tokens->reserve(parsed.size()); - for (std::string &p: parsed) { - m_tokens->push_back(p); - } - } - - for (int i = 0; i < m_argsLen; i++) { - ConvertArg(args[i], i); - } - } -} - -JSToJavaConverter::JSToJavaConverter( - v8::Isolate* isolate, - const v8::FunctionCallbackInfo &args, - const string &methodSignature, - MetadataEntry *entry, - const v8::Local kotlinExtensionFunctionThis) - : - m_isolate(isolate), - m_methodSignature(methodSignature) { - - int v8ProvidedArgumentsLength = args.Length(); - m_argsLen = 1 + v8ProvidedArgumentsLength; - - if (m_argsLen > 0) { - if ((entry != nullptr) && (entry->isResolved)) { - if (entry->parsedSig.empty()) { - JniSignatureParser parser(m_methodSignature); - entry->parsedSig = parser.Parse(); - } - m_tokens = &entry->parsedSig; - } else { - JniSignatureParser parser(m_methodSignature); - auto parsed = parser.Parse(); - m_tokens = new std::vector(); - m_tokens->reserve(parsed.size()); - for (std::string &p: parsed) { - m_tokens->push_back(p); - } - } - - ConvertArg(kotlinExtensionFunctionThis, 0); - - for (int i = 0; i < v8ProvidedArgumentsLength; i++) { - ConvertArg(args[i], i + 1); - } - } -} - -JSToJavaConverter::JSToJavaConverter( - v8::Isolate* isolate, - const v8::FunctionCallbackInfo &args, - const string &methodSignature) - : - m_isolate(isolate), - m_methodSignature(methodSignature) { - m_argsLen = args.Length(); - - JniSignatureParser parser(m_methodSignature); - auto parsed = parser.Parse(); - m_tokens = new std::vector(); - m_tokens->reserve(parsed.size()); - for (std::string &p: parsed) { - m_tokens->push_back(p); - } - - for (int i = 0; i < m_argsLen; i++) { - ConvertArg(args[i], i); - } -} - -bool JSToJavaConverter::ConvertArg(const Local &arg, int index) { - - - if (arg->IsNumber() || arg->IsNumberObject()) { - return tns::ConvertJavaScriptNumber( - m_isolate, - arg, - index, - m_tokens, - m_jniArgRefsState - ); - } else if (arg->IsString() || arg->IsStringObject()) { - return tns::ConvertJavaScriptString( - arg, - index, - m_jniArgRefsState - ); - } else if (arg->IsBoolean() || arg->IsBooleanObject()) { - return tns::ConvertJavaScriptBoolean( - m_isolate, - arg, - index, - m_tokens, - m_jniArgRefsState - ); - } else if (arg->IsArray() && m_tokens->at(index)[0] == '[') { - return tns::ConvertJavaScriptArray( - m_isolate, - arg, - index, - m_tokens, - m_jniArgRefsState - ); - } else if (arg->IsObject()) { - return tns::ConvertJavaScriptObject( - m_isolate, - arg, - index, - m_methodSignature, - m_tokens, - - m_jniArgRefsState - ); - } else if (arg->IsUndefined() || arg->IsNull()) { - m_jniArgRefsState.SetConvertedObject(index, nullptr); - return true; - } - - m_jniArgRefsState.SetConvertedObject(index, nullptr); - return false; - -} - -int JSToJavaConverter::Length() const { - return m_argsLen; -} - -jvalue *JSToJavaConverter::ToArgs() { - return m_jniArgRefsState.m_args; -} - -JSToJavaConverter::~JSToJavaConverter() { - if (m_argsLen > 0) { - JEnv env; - auto refsSize = m_jniArgRefsState.m_args_refs_size; - auto argsRefs = m_jniArgRefsState.m_args_refs; - auto args = m_jniArgRefsState.m_args; - - for (int i = 0; i < refsSize; i++) { - int index = argsRefs[i]; - if (index != -1) { - env.DeleteLocalRef(args[index].l); - } - } - } -} diff --git a/test-app/runtime/src/main/cpp/conversions/JSToJavaConverter.h b/test-app/runtime/src/main/cpp/conversions/JSToJavaConverter.h deleted file mode 100644 index 6dd13314a..000000000 --- a/test-app/runtime/src/main/cpp/conversions/JSToJavaConverter.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef TEST_APP_JSTOJAVACONVERTER_H -#define TEST_APP_JSTOJAVACONVERTER_H - -#include -#include -#include "JEnv.h" -#include "v8.h" -#include "MetadataEntry.h" -#include "array" -#include "ConversionConsts.h" -#include "JNIArgRefsState.h" - -namespace tns { - - class JSToJavaConverter { - public: - - JSToJavaConverter( - v8::Isolate* isolate, - const v8::FunctionCallbackInfo &args, - const std::string &methodSignature, - MetadataEntry *entry); - - JSToJavaConverter( - v8::Isolate* isolate, - const v8::FunctionCallbackInfo &args, - const std::string &methodSignature, - MetadataEntry *entry, - const v8::Local kotlinExtensionFunctionThis); - - JSToJavaConverter( - v8::Isolate* isolate, - const v8::FunctionCallbackInfo &args, - const std::string &methodSignature); - - ~JSToJavaConverter(); - - jvalue *ToArgs(); - - int Length() const; - - private: - - bool ConvertArg(const v8::Local &arg, int index); - - v8::Isolate *m_isolate; - std::string m_methodSignature; - std::vector *m_tokens; - int m_argsLen; - JNIArgRefsState m_jniArgRefsState; - }; -} - - -#endif //TEST_APP_JSTOJAVACONVERTER_H diff --git a/test-app/runtime/src/main/cpp/conversions/arrays/JSToJavaArraysConverter.cpp b/test-app/runtime/src/main/cpp/conversions/arrays/JSToJavaArraysConverter.cpp deleted file mode 100644 index 83305e035..000000000 --- a/test-app/runtime/src/main/cpp/conversions/arrays/JSToJavaArraysConverter.cpp +++ /dev/null @@ -1,171 +0,0 @@ -// -// Created by Vladimir Mutafov on 16.11.22. -// - -#include "JSToJavaArraysConverter.h" -#include "ObjectManager.h" -#include "JniSignatureParser.h" -#include "JsArgToArrayConverter.h" -#include "ArgConverter.h" -#include "V8StringConstants.h" -#include "NumericCasts.h" -#include "NativeScriptException.h" -#include "Runtime.h" -#include "V8GlobalHelpers.h" -#include -#include "../CastTypes.h" -#include "string" - -bool tns::ConvertJavaScriptArray( - v8::Isolate *isolate, - const Local &arg, - int index, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState -) { - bool success = true; - - jarray arr = nullptr; - - const Local &jsArr = Local::Cast(arg); - - jsize arrLength = jsArr->Length(); // java arrays size is max int32 - - const auto &arraySignature = tokens->at(index); - auto context = isolate->GetCurrentContext(); - - std::string elementType = arraySignature.substr(1); - - const char elementTypePrefix = elementType[0]; - - jclass elementClass; - std::string strippedClassName; - - JEnv env; - switch (elementTypePrefix) { - case 'Z': { - arr = env.NewBooleanArray(arrLength); - jboolean bools[arrLength]; - for (jsize i = 0; i < arrLength; i++) { - bools[i] = jsArr - ->Get(context, i) - .ToLocalChecked() - ->BooleanValue(isolate); - } - env.SetBooleanArrayRegion((jbooleanArray) arr, 0, arrLength, bools); - break; - } - case 'B': { - arr = env.NewByteArray(arrLength); - jbyte bytes[arrLength]; - for (jsize i = 0; i < arrLength; i++) { - bytes[i] = jsArr - ->Get(context, i) - .ToLocalChecked() - ->Int32Value(context) - .ToChecked(); - } - env.SetByteArrayRegion((jbyteArray) arr, 0, arrLength, bytes); - break; - } - case 'C': { - arr = env.NewCharArray(arrLength); - jchar chars[arrLength]; - for (jsize i = 0; i < arrLength; i++) { - String::Utf8Value utf8(isolate, jsArr->Get(context, i).ToLocalChecked()->ToString( - context).ToLocalChecked()); - JniLocalRef s(env.NewString((jchar *) *utf8, 1)); - const char *singleChar = env.GetStringUTFChars(s, nullptr); - chars[i] = *singleChar; - env.ReleaseStringUTFChars(s, singleChar); - } - env.SetCharArrayRegion((jcharArray) arr, 0, arrLength, chars); - break; - } - case 'S': { - arr = env.NewShortArray(arrLength); - jshort shorts[arrLength]; - for (jsize i = 0; i < arrLength; i++) { - shorts[i] = jsArr - ->Get(context, i) - .ToLocalChecked() - ->Int32Value(context) - .ToChecked(); - } - env.SetShortArrayRegion((jshortArray) arr, 0, arrLength, shorts); - break; - } - case 'I': { - arr = env.NewIntArray(arrLength); - jint ints[arrLength]; - for (jsize i = 0; i < arrLength; i++) { - ints[i] = jsArr - ->Get(context, i) - .ToLocalChecked() - ->Int32Value(context) - .ToChecked(); - } - env.SetIntArrayRegion((jintArray) arr, 0, arrLength, ints); - break; - } - case 'J': { - arr = env.NewLongArray(arrLength); - jlong longs[arrLength]; - for (jsize i = 0; i < arrLength; i++) { - longs[i] = jsArr - ->Get(context, i) - .ToLocalChecked() - ->NumberValue(context) - .ToChecked(); - } - env.SetLongArrayRegion((jlongArray) arr, 0, arrLength, longs); - break; - } - case 'F': { - arr = env.NewFloatArray(arrLength); - jfloat floats[arrLength]; - for (jsize i = 0; i < arrLength; i++) { - floats[i] = jsArr - ->Get(context, i) - .ToLocalChecked() - ->NumberValue(context) - .ToChecked(); - } - env.SetFloatArrayRegion((jfloatArray) arr, 0, arrLength, floats); - break; - } - case 'D': { - arr = env.NewDoubleArray(arrLength); - jdouble doubles[arrLength]; - for (jsize i = 0; i < arrLength; i++) { - doubles[i] = jsArr - ->Get(context, i) - .ToLocalChecked() - ->NumberValue(context) - .ToChecked(); - } - env.SetDoubleArrayRegion((jdoubleArray) arr, 0, arrLength, doubles); - break; - } - case 'L': - strippedClassName = elementType.substr(1, elementType.length() - 2); - elementClass = env.FindClass(strippedClassName); - arr = env.NewObjectArray(arrLength, elementClass, nullptr); - for (int i = 0; i < arrLength; i++) { - auto v = jsArr->Get(context, i).ToLocalChecked(); - JsArgToArrayConverter c(context, v, false, (int) Type::Null); - jobject o = c.GetConvertedArg(); - env.SetObjectArrayElement((jobjectArray) arr, i, o); - } - break; - default: - success = false; - break; - } - - if (success) { - jniArgRefsState.SetConvertedObject(index, arr); - } - - return success; -} \ No newline at end of file diff --git a/test-app/runtime/src/main/cpp/conversions/arrays/JSToJavaArraysConverter.h b/test-app/runtime/src/main/cpp/conversions/arrays/JSToJavaArraysConverter.h deleted file mode 100644 index b555f3fb1..000000000 --- a/test-app/runtime/src/main/cpp/conversions/arrays/JSToJavaArraysConverter.h +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by Vladimir Mutafov on 16.11.22. -// - -#ifndef TEST_APP_JSTOJAVAARRAYSCONVERTER_H -#define TEST_APP_JSTOJAVAARRAYSCONVERTER_H - -#include "v8.h" -#include "../JNIArgRefsState.h" - -namespace tns { - bool ConvertJavaScriptArray( - v8::Isolate *isolate, - const v8::Local &jsValue, - int index, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState - ); -} - -#endif //TEST_APP_JSTOJAVAARRAYSCONVERTER_H diff --git a/test-app/runtime/src/main/cpp/conversions/objects/JSToJavaObjectsConverter.cpp b/test-app/runtime/src/main/cpp/conversions/objects/JSToJavaObjectsConverter.cpp deleted file mode 100644 index d9ad333d2..000000000 --- a/test-app/runtime/src/main/cpp/conversions/objects/JSToJavaObjectsConverter.cpp +++ /dev/null @@ -1,282 +0,0 @@ -// -// Created by Vladimir Mutafov on 16.11.22. -// - -#include "JSToJavaObjectsConverter.h" -#include "ObjectManager.h" -#include "JniSignatureParser.h" -#include "JsArgToArrayConverter.h" -#include "ArgConverter.h" -#include "V8StringConstants.h" -#include "NumericCasts.h" -#include "NativeScriptException.h" -#include "Runtime.h" -#include "V8GlobalHelpers.h" -#include -#include "../CastTypes.h" -#include "string" - -bool tns::ConvertJavaScriptObject( - v8::Isolate *const isolate, - const v8::Local &arg, - int index, - std::string &methodSignature, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState -) { - auto context = isolate->GetCurrentContext(); - auto jsObject = arg->ToObject(context).ToLocalChecked(); - - auto castType = NumericCasts::GetCastType(isolate, jsObject); - - Local castValue; - JniLocalRef obj; - - auto runtime = Runtime::GetRuntime(isolate); - auto objectManager = runtime->GetObjectManager(); - - JEnv env; - - switch (castType) { - case CastType::Char: - castValue = NumericCasts::GetCastValue(jsObject); - if (castValue->IsString()) { - std::string value = ArgConverter::ConvertToString( - castValue->ToString(context).ToLocalChecked()); - jniArgRefsState.m_args[index].c = (jchar) value[0]; - return true; - } - break; - - case CastType::Byte: - castValue = NumericCasts::GetCastValue(jsObject); - if (castValue->IsString()) { - std::string strValue = ArgConverter::ConvertToString( - castValue->ToString(context).ToLocalChecked()); - int byteArg = atoi(strValue.c_str()); - jbyte value = (jbyte) byteArg; - ConvertFromCastFunctionObject(value, index, methodSignature, tokens, jniArgRefsState); - } else if (castValue->IsInt32()) { - jbyte value = (jbyte) castValue->ToInt32(context).ToLocalChecked()->Int32Value( - context).ToChecked(); - ConvertFromCastFunctionObject(value, index, methodSignature, tokens, jniArgRefsState); - } - break; - - case CastType::Short: - castValue = NumericCasts::GetCastValue(jsObject); - if (castValue->IsString()) { - std::string strValue = ArgConverter::ConvertToString( - castValue->ToString(context).ToLocalChecked()); - int shortArg = atoi(strValue.c_str()); - jshort value = (jshort) shortArg; - ConvertFromCastFunctionObject(value, index, methodSignature, tokens, jniArgRefsState); - } else if (castValue->IsInt32()) { - jshort value = (jshort) castValue->ToInt32(context).ToLocalChecked()->Int32Value( - context).ToChecked(); - ConvertFromCastFunctionObject(value, index, methodSignature, tokens, jniArgRefsState); - } - break; - - case CastType::Long: - castValue = NumericCasts::GetCastValue(jsObject); - if (castValue->IsString()) { - std::string strValue = ArgConverter::ConvertToString( - castValue->ToString(context).ToLocalChecked()); - int64_t longArg = atoll(strValue.c_str()); - jlong value = (jlong) longArg; - ConvertFromCastFunctionObject(value, index, methodSignature, tokens, jniArgRefsState); - } else if (castValue->IsInt32()) { - jlong value = (jlong) castValue->ToInt32(context).ToLocalChecked()->IntegerValue( - context).ToChecked(); - ConvertFromCastFunctionObject(value, index, methodSignature, tokens, jniArgRefsState); - } - break; - - case CastType::Float: - castValue = NumericCasts::GetCastValue(jsObject); - if (castValue->IsNumber()) { - double floatArg = castValue->ToNumber(context).ToLocalChecked()->NumberValue( - context).ToChecked(); - jfloat value = (jfloat) floatArg; - ConvertFromCastFunctionObject(value, index, methodSignature, tokens, jniArgRefsState); - } - break; - - case CastType::Double: - castValue = NumericCasts::GetCastValue(jsObject); - if (castValue->IsNumber()) { - double doubleArg = castValue->ToNumber(context).ToLocalChecked()->NumberValue( - context).ToChecked(); - jdouble value = (jdouble) doubleArg; - ConvertFromCastFunctionObject(value, index, methodSignature, tokens, jniArgRefsState); - } - break; - - - case CastType::None: - obj = objectManager->GetJavaObjectByJsObject(jsObject); - - if (obj.IsNull() && (jsObject->IsTypedArray() || jsObject->IsArrayBuffer() || - jsObject->IsArrayBufferView() || - jsObject->IsSharedArrayBuffer())) { - - JavaObjectCastType bufferCastType = tns::JavaObjectCastType::Byte; - std::shared_ptr store; - size_t offset = 0; - size_t length; - uint8_t *data = nullptr; - auto link_with_data = false; - if (jsObject->IsArrayBuffer()) { - auto array = jsObject.As(); - store = array->GetBackingStore(); - length = array->ByteLength(); - } else if (jsObject->IsSharedArrayBuffer()) { - auto array = jsObject.As(); - store = array->GetBackingStore(); - length = array->ByteLength(); - } else if (jsObject->IsArrayBufferView()) { - auto array = jsObject.As(); - - if (!array->HasBuffer()) { - - length = array->ByteLength(); - void *data_ = malloc(length); - array->CopyContents(data_, length); - data = (uint8_t *) data_; - link_with_data = true; - } else { - length = array->ByteLength(); - } - offset = array->ByteOffset(); - store = array->Buffer()->GetBackingStore(); - bufferCastType = tns::GetCastType(array); - } else { - auto array = jsObject.As(); - offset = array->ByteOffset(); - store = array->Buffer()->GetBackingStore(); - length = array->ByteLength(); - bufferCastType = tns::GetCastType(array); - } - - if (data == nullptr) { - data = static_cast(store->Data()) + offset; - } - - auto directBuffer = env.NewDirectByteBuffer( - data, - length); - - auto directBufferClazz = env.GetObjectClass(directBuffer); - - auto byteOrderId = env.GetMethodID(directBufferClazz, "order", - "(Ljava/nio/ByteOrder;)Ljava/nio/ByteBuffer;"); - - auto byteOrderClazz = env.FindClass("java/nio/ByteOrder"); - - auto byteOrderEnumId = env.GetStaticMethodID(byteOrderClazz, "nativeOrder", - "()Ljava/nio/ByteOrder;"); - - auto nativeByteOrder = env.CallStaticObjectMethodA(byteOrderClazz, byteOrderEnumId, - nullptr); - - directBuffer = env.CallObjectMethod(directBuffer, byteOrderId, nativeByteOrder); - - jobject buffer; - - if (bufferCastType == JavaObjectCastType::Short) { - - auto id = env.GetMethodID(directBufferClazz, "asShortBuffer", - "()Ljava/nio/ShortBuffer;"); - buffer = env.CallObjectMethodA(directBuffer, id, nullptr); - } else if (bufferCastType == JavaObjectCastType::Int) { - - auto id = env.GetMethodID(directBufferClazz, "asIntBuffer", - "()Ljava/nio/IntBuffer;"); - buffer = env.CallObjectMethodA(directBuffer, id, nullptr); - } else if (bufferCastType == JavaObjectCastType::Long) { - - auto id = env.GetMethodID(directBufferClazz, "asLongBuffer", - "()Ljava/nio/LongBuffer;"); - buffer = env.CallObjectMethodA(directBuffer, id, nullptr); - } else if (bufferCastType == JavaObjectCastType::Float) { - - auto id = env.GetMethodID(directBufferClazz, "asFloatBuffer", - "()Ljava/nio/FloatBuffer;"); - buffer = env.CallObjectMethodA(directBuffer, id, nullptr); - } else if (bufferCastType == JavaObjectCastType::Double) { - - auto id = env.GetMethodID(directBufferClazz, "asDoubleBuffer", - "()Ljava/nio/DoubleBuffer;"); - buffer = env.CallObjectMethodA(directBuffer, id, nullptr); - } else { - buffer = directBuffer; - } - - buffer = env.NewGlobalRef(buffer); - - int id = objectManager->GetOrCreateObjectId(buffer); - auto clazz = env.GetObjectClass(buffer); - objectManager->Link(jsObject, id, clazz); - - obj = objectManager->GetJavaObjectByJsObject(jsObject); - } - - V8GetPrivateValue(isolate, jsObject, ArgConverter::ConvertToV8String(isolate, - V8StringConstants::NULL_NODE_NAME), - castValue); - - if (!castValue.IsEmpty()) { - jniArgRefsState.SetConvertedObject(index, nullptr); - break; - } - - if (!obj.IsNull()) { - jniArgRefsState.SetConvertedObject(index, obj.Move(), obj.IsGlobal()); - } - break; - - default: - throw NativeScriptException("Unsupported cast type"); - } - - return true; -} - - -template -bool tns::ConvertFromCastFunctionObject( - T value, - int index, - std::string &methodSignature, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState -) { - const auto &typeSignature = tokens->at(index); - const char typeSignaturePrefix = typeSignature[0]; - - switch (typeSignaturePrefix) { - case 'B': - jniArgRefsState.m_args[index].b = (jbyte) value; - break; - case 'S': - jniArgRefsState.m_args[index].s = (jshort) value; - break; - case 'I': - jniArgRefsState.m_args[index].i = (jint) value; - break; - case 'J': - jniArgRefsState.m_args[index].j = (jlong) value; - break; - case 'F': - jniArgRefsState.m_args[index].f = (jfloat) value; - break; - case 'D': - jniArgRefsState.m_args[index].d = (jdouble) value; - break; - default: - return false; - } - - return true; -} \ No newline at end of file diff --git a/test-app/runtime/src/main/cpp/conversions/objects/JSToJavaObjectsConverter.h b/test-app/runtime/src/main/cpp/conversions/objects/JSToJavaObjectsConverter.h deleted file mode 100644 index 4ac824172..000000000 --- a/test-app/runtime/src/main/cpp/conversions/objects/JSToJavaObjectsConverter.h +++ /dev/null @@ -1,36 +0,0 @@ -// -// Created by Vladimir Mutafov on 16.11.22. -// - -#ifndef TEST_APP_JSTOJAVAOBJECTSCONVERTER_H -#define TEST_APP_JSTOJAVAOBJECTSCONVERTER_H - -#include -#include "v8.h" -#include "string" -#include "vector" -#include "array" -#include "../ConversionConsts.h" -#include "../JNIArgRefsState.h" - -namespace tns { - bool ConvertJavaScriptObject( - v8::Isolate *isolate, - const v8::Local &jsValue, - int index, - std::string &methodSignature, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState - ); - - template - bool ConvertFromCastFunctionObject( - T value, - int index, - std::string &methodSignature, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState - ); -} - -#endif //TEST_APP_JSTOJAVAOBJECTSCONVERTER_H diff --git a/test-app/runtime/src/main/cpp/conversions/primitives/JSToJavaPrimitivesConverter.cpp b/test-app/runtime/src/main/cpp/conversions/primitives/JSToJavaPrimitivesConverter.cpp deleted file mode 100644 index 986a150c3..000000000 --- a/test-app/runtime/src/main/cpp/conversions/primitives/JSToJavaPrimitivesConverter.cpp +++ /dev/null @@ -1,128 +0,0 @@ -// -// Created by Vladimir Mutafov on 16.11.22. -// - -#include "JSToJavaPrimitivesConverter.h" -#include "ObjectManager.h" -#include "JniSignatureParser.h" -#include "JsArgToArrayConverter.h" -#include "ArgConverter.h" -#include "V8StringConstants.h" -#include "NumericCasts.h" -#include "NativeScriptException.h" -#include "Runtime.h" -#include "V8GlobalHelpers.h" -#include -#include "../CastTypes.h" -#include "string" - -bool tns::ConvertJavaScriptBoolean( - v8::Isolate *isolate, - const v8::Local &jsValue, - int index, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState -) { - bool success; - - const auto &typeSignature = tokens->at(index); - auto context = isolate->GetCurrentContext(); - - if (typeSignature == "Z") { - bool argValue; - if (jsValue->IsBoolean()) { - argValue = jsValue->BooleanValue(isolate); - } else { - auto boolObj = Local::Cast(jsValue); - Local val; - boolObj->Get(context, V8StringConstants::GetValueOf(isolate)).ToLocal(&val); - if (!val.IsEmpty() && val->IsFunction()) { - argValue = val.As()->Call(context, boolObj, 0, - nullptr).ToLocalChecked()->BooleanValue( - isolate); - } else { - argValue = false; - } - } - - jboolean value = argValue ? JNI_TRUE : JNI_FALSE; - jniArgRefsState.m_args[index].z = value; - success = true; - } else { - success = false; - } - - return success; -} - -bool tns::ConvertJavaScriptNumber( - v8::Isolate *isolate, - const v8::Local &jsValue, - int index, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState -) { - bool success = true; - - jvalue value = { - 0 - }; - - const auto &typeSignature = tokens->at(index); - auto context = isolate->GetCurrentContext(); - - const char typePrefix = typeSignature[0]; - - switch (typePrefix) { - case 'B': // byte - value.b = (jbyte) jsValue->Int32Value(context).ToChecked(); - break; - - case 'S': // short - value.s = (jshort) jsValue->Int32Value(context).ToChecked(); - break; - - case 'I': // int - value.i = (jint) jsValue->Int32Value(context).ToChecked(); - break; - - case 'J': // long - if (jsValue->IsNumberObject()) { - auto numObj = Local::Cast(jsValue); - value.j = (jlong) numObj->ValueOf(); - } else { - value.j = (jlong) jsValue->IntegerValue(context).ToChecked(); - } - break; - - case 'F': // float - value.f = (jfloat) jsValue->NumberValue(context).ToChecked(); - break; - - case 'D': // double - value.d = (jdouble) jsValue->NumberValue(context).ToChecked(); - break; - - default: - success = false; - break; - } - - if (success) { - jniArgRefsState.m_args[index] = value; - } - - return success; -} - -bool tns::ConvertJavaScriptString( - const Local &jsValue, - int index, - tns::JNIArgRefsState &jniArgRefsState -) { - jstring stringObject = ArgConverter::ConvertToJavaString(jsValue); - jniArgRefsState.SetConvertedObject(index, stringObject); - - return true; -} - diff --git a/test-app/runtime/src/main/cpp/conversions/primitives/JSToJavaPrimitivesConverter.h b/test-app/runtime/src/main/cpp/conversions/primitives/JSToJavaPrimitivesConverter.h deleted file mode 100644 index cb8ab33f0..000000000 --- a/test-app/runtime/src/main/cpp/conversions/primitives/JSToJavaPrimitivesConverter.h +++ /dev/null @@ -1,35 +0,0 @@ -// -// Created by Vladimir Mutafov on 16.11.22. -// - -#ifndef TEST_APP_JSTOJAVAPRIMITIVESCONVERTER_H -#define TEST_APP_JSTOJAVAPRIMITIVESCONVERTER_H - -#include "v8.h" -#include "../JNIArgRefsState.h" - -namespace tns { - bool ConvertJavaScriptBoolean( - v8::Isolate *isolate, - const v8::Local &jsValue, - int index, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState - ); - - bool ConvertJavaScriptNumber( - v8::Isolate *isolate, - const v8::Local &jsValue, - int index, - std::vector *tokens, - JNIArgRefsState &jniArgRefsState - ); - - bool ConvertJavaScriptString( - const v8::Local &jsValue, - int index, - JNIArgRefsState &jniArgRefsState - ); -} - -#endif //TEST_APP_JSTOJAVAPRIMITIVESCONVERTER_H