Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ LOCAL_SRC_FILES := com_tns_AssetExtractor.cpp com_tns_Platform.cpp com_tns_JsDeb
FieldAccessor.cpp ArrayElementAccessor.cpp \
ExceptionUtil.cpp Util.cpp Logger.cpp Profiler.cpp \
ObjectManager.cpp NumericCasts.cpp WeakRef.cpp \
MetadataMethodInfo.cpp SimpleProfiler.cpp JType.cpp File.cpp Require.cpp
MetadataMethodInfo.cpp SimpleProfiler.cpp JType.cpp File.cpp Require.cpp Constants.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_LDLIBS := -llog -landroid -lz
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
Expand Down
16 changes: 0 additions & 16 deletions src/jni/ArgConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,6 @@ namespace tns

static bool ReadJStringInBuffer(jstring value, jsize& utfLength);

static jlong ObjectToLong(jobject object);

static jboolean ObjectToBoolean(jobject object);

static jchar ObjectToChar(jobject object);

static jbyte ObjectToByte(jobject object);

static jshort ObjectToShort(jobject object);

static jint ObjectToInt(jobject object);

static jfloat ObjectToFloat(jobject object);

static jdouble ObjectToDouble(jobject object);

static jstring ObjectToString(jobject object);

static v8::Local<v8::String> jcharToV8String(jchar value);
Expand Down
12 changes: 12 additions & 0 deletions src/jni/Constants.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Constants.cpp
*
* Created on: Nov 6, 2015
* Author: gatanasov
*/

#include "Constants.h"

std::string Constants::APP_ROOT_FOLDER_PATH = "";
bool Constants::V8_CACHE_COMPILED_CODE = false;
std::string Constants::V8_STARTUP_FLAGS = "";
6 changes: 6 additions & 0 deletions src/jni/Constants.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef CONSTANTS_H_
#define CONSTANTS_H_

#include <string>

class Constants
{
public:
Expand All @@ -12,6 +14,10 @@ class Constants

static std::string APP_ROOT_FOLDER_PATH;

static std::string V8_STARTUP_FLAGS;

static bool V8_CACHE_COMPILED_CODE;

private:
Constants() {}
};
Expand Down
6 changes: 6 additions & 0 deletions src/jni/File.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

#include "File.h"
#include <sstream>
#include <fstream>

using namespace std;

namespace tns
{
bool File::Exists(const string& path)
{
std::ifstream infile(path.c_str());
return infile.good();
}
string File::ReadText(const string& filePath)
{
int len;
Expand Down
1 change: 1 addition & 0 deletions src/jni/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace tns
public:
static const char* ReadText(const std::string& filePath, int& length, bool& isNew);
static std::string ReadText(const std::string& filePath);
static bool Exists(const std::string& filePath);
private:
static const int BUFFER_SIZE = 1024 * 1024;
static char* Buffer;
Expand Down
7 changes: 7 additions & 0 deletions src/jni/JniLocalRef.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "JniLocalRef.h"
#include "JType.h"
#include <cassert>

using namespace v8;
Expand Down Expand Up @@ -70,6 +71,12 @@ JniLocalRef::operator jclass() const
return (jclass)m_obj;
}

JniLocalRef::operator jboolean() const
{
JEnv env;
return JType::BooleanValue(env, m_obj);
}


JniLocalRef::operator jthrowable() const
{
Expand Down
2 changes: 2 additions & 0 deletions src/jni/JniLocalRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace tns

operator jobject() const;

operator jboolean() const;

operator jclass() const;

operator jstring() const;
Expand Down
164 changes: 1 addition & 163 deletions src/jni/NativeScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "JsArgToArrayConverter.h"
#include "ArgConverter.h"
#include "v8-profiler.h"
#include "Constants.h"
#include <assert.h>
#include <iostream>
#include <sstream>
Expand All @@ -29,6 +28,7 @@ using namespace tns;
void NativeScriptRuntime::Init(JavaVM *jvm, ObjectManager *objectManager)
{
NativeScriptRuntime::jvm = jvm;
Require::Init();

JEnv env;

Expand All @@ -38,9 +38,6 @@ void NativeScriptRuntime::Init(JavaVM *jvm, ObjectManager *objectManager)
PlatformClass = env.FindClass("com/tns/Platform");
assert(PlatformClass != nullptr);

RequireClass = env.FindClass("com/tns/Require");
assert(RequireClass != nullptr);

RESOLVE_CLASS_METHOD_ID = env.GetStaticMethodID(PlatformClass, "resolveClass", "(Ljava/lang/String;[Ljava/lang/String;)Ljava/lang/Class;");
assert(RESOLVE_CLASS_METHOD_ID != nullptr);

Expand All @@ -56,9 +53,6 @@ void NativeScriptRuntime::Init(JavaVM *jvm, ObjectManager *objectManager)
APP_FAIL_METHOD_ID = env.GetStaticMethodID(PlatformClass, "appFail", "(Ljava/lang/Throwable;Ljava/lang/String;)V");
assert(APP_FAIL_METHOD_ID != nullptr);

GET_MODULE_PATH_METHOD_ID = env.GetStaticMethodID(RequireClass, "getModulePath", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
assert(GET_MODULE_PATH_METHOD_ID != nullptr);

ENABLE_VERBOSE_LOGGING_METHOD_ID = env.GetStaticMethodID(PlatformClass, "enableVerboseLogging", "()V");
assert(ENABLE_VERBOSE_LOGGING_METHOD_ID != nullptr);

Expand Down Expand Up @@ -753,164 +747,12 @@ void NativeScriptRuntime::AppFail(jthrowable throwable, const char *message)
}


void NativeScriptRuntime::AddApplicationModule(const string& appName, v8::Persistent<v8::Object>* applicationModule)
{
loadedModules.insert(make_pair(appName, applicationModule));
}

void NativeScriptRuntime::CreateGlobalCastFunctions(const Local<ObjectTemplate>& globalTemplate)
{
castFunctions.CreateGlobalCastFunctions(globalTemplate);
}


void NativeScriptRuntime::CompileAndRun(string modulePath, bool& hasError, Local<Object>& moduleObj)
{
auto isolate = Isolate::GetCurrent();

Local<Value> exportObj = Object::New(isolate);
auto tmpExportObj = new Persistent<Object>(isolate, exportObj.As<Object>());
loadedModules.insert(make_pair(modulePath, tmpExportObj));

TryCatch tc;

auto scriptText = Require::LoadModule(modulePath);

DEBUG_WRITE("Compiling script (module %s)", modulePath.c_str());
auto fullRequiredModulePath = ConvertToV8String(modulePath);
auto script = Script::Compile(scriptText, fullRequiredModulePath);
DEBUG_WRITE("Compiled script (module %s)", modulePath.c_str());

if (ExceptionUtil::GetInstance()->HandleTryCatch(tc, "Script " + modulePath + " contains compilation errors!"))
{
hasError = true;
}
else if (script.IsEmpty())
{
//think about more descriptive message -> [script_name] was empty
DEBUG_WRITE("%s was empty", modulePath.c_str());
}
else
{
DEBUG_WRITE("Running script (module %s)", modulePath.c_str());

auto f = script->Run().As<Function>();
if (ExceptionUtil::GetInstance()->HandleTryCatch(tc, "Error running script " + modulePath))
{
hasError = true;
}
else
{
auto result = f->Call(Object::New(isolate), 1, &exportObj);
if(ExceptionUtil::GetInstance()->HandleTryCatch(tc, "Error calling module function "))
{
hasError = true;
}
else
{
moduleObj = result.As<Object>();
if (moduleObj.IsEmpty())
{
auto objectTemplate = ObjectTemplate::New();
moduleObj = objectTemplate->NewInstance();
}

DEBUG_WRITE("Script completed (module %s)", modulePath.c_str());

if (!moduleObj->StrictEquals(exportObj))
{
loadedModules.erase(modulePath);
tmpExportObj->Reset();
delete tmpExportObj;

auto persistentModuleObject = new Persistent<Object>(isolate, moduleObj.As<Object>());

loadedModules.insert(make_pair(modulePath, persistentModuleObject));
}
}
}
}

if(hasError)
{
loadedModules.erase(modulePath);
tmpExportObj->Reset();
delete tmpExportObj;

// this handles recursive require calls
tc.ReThrow();
}
}

void NativeScriptRuntime::RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
{
SET_PROFILER_FRAME();

auto isolate = Isolate::GetCurrent();

if (args.Length() != 2)
{
isolate->ThrowException(ConvertToV8String("require should be called with two parameters"));
return;
}
if (!args[0]->IsString())
{
isolate->ThrowException(ConvertToV8String("require's first parameter should be string"));
return;
}
if (!args[1]->IsString())
{
isolate->ThrowException(ConvertToV8String("require's second parameter should be string"));
return;
}

string moduleName = ConvertToString(args[0].As<String>());
string callingModuleDirName = ConvertToString(args[1].As<String>());

JEnv env;
JniLocalRef jsModulename(env.NewStringUTF(moduleName.c_str()));
JniLocalRef jsCallingModuleDirName(env.NewStringUTF(callingModuleDirName.c_str()));
JniLocalRef jsModulePath(env.CallStaticObjectMethod(RequireClass, GET_MODULE_PATH_METHOD_ID, (jstring) jsModulename, (jstring) jsCallingModuleDirName));

// cache the required modules by full path, not name only, since there might be some collisions with relative paths and names
string modulePath = ArgConverter::jstringToString((jstring) jsModulePath);
if(modulePath == ""){
// module not found
stringstream ss;
ss << "Module \"" << moduleName << "\" not found";
string exception = ss.str();
ExceptionUtil::GetInstance()->ThrowExceptionToJs(exception);
return;
}
if (modulePath == "EXTERNAL_FILE_ERROR")
{
// module not found
stringstream ss;
ss << "Module \"" << moduleName << "\" is located on the external storage. Modules can be private application files ONLY";
string exception = ss.str();
ExceptionUtil::GetInstance()->ThrowExceptionToJs(exception);
return;
}

auto it = loadedModules.find(modulePath);

Local<Object> moduleObj;
bool hasError = false;

if (it == loadedModules.end())
{
CompileAndRun(modulePath, hasError, moduleObj);
}
else
{
moduleObj = Local<Object>::New(isolate, *((*it).second));
}

if(!hasError){
args.GetReturnValue().Set(moduleObj);
}
}

vector<string> NativeScriptRuntime::GetTypeMetadata(const string& name, int index)
{
JEnv env;
Expand Down Expand Up @@ -1108,23 +950,19 @@ int NativeScriptRuntime::GetArrayLength(const Local<Object>& arr)

JavaVM* NativeScriptRuntime::jvm = nullptr;
jclass NativeScriptRuntime::PlatformClass = nullptr;
jclass NativeScriptRuntime::RequireClass = nullptr;
jclass NativeScriptRuntime::JAVA_LANG_STRING = nullptr;
jmethodID NativeScriptRuntime::RESOLVE_CLASS_METHOD_ID = nullptr;
jmethodID NativeScriptRuntime::CREATE_INSTANCE_METHOD_ID = nullptr;
jmethodID NativeScriptRuntime::CACHE_CONSTRUCTOR_METHOD_ID = nullptr;
jmethodID NativeScriptRuntime::APP_FAIL_METHOD_ID = nullptr;
jmethodID NativeScriptRuntime::GET_MODULE_PATH_METHOD_ID = nullptr;
jmethodID NativeScriptRuntime::GET_TYPE_METADATA = nullptr;
jmethodID NativeScriptRuntime::ENABLE_VERBOSE_LOGGING_METHOD_ID = nullptr;
jmethodID NativeScriptRuntime::DISABLE_VERBOSE_LOGGING_METHOD_ID = nullptr;
jmethodID NativeScriptRuntime::GET_CHANGE_IN_BYTES_OF_USED_MEMORY_METHOD_ID = nullptr;
map<string, Persistent<Object>*> NativeScriptRuntime::loadedModules;
MetadataTreeNode* NativeScriptRuntime::metadataRoot = nullptr;
ObjectManager* NativeScriptRuntime::objectManager = nullptr;
NumericCasts NativeScriptRuntime::castFunctions;
ArrayElementAccessor NativeScriptRuntime::arrayElementAccessor;
FieldAccessor NativeScriptRuntime::fieldAccessor;
string NativeScriptRuntime::APP_FILES_DIR;
map<string, int> NativeScriptRuntime::s_constructorCache;
map<std::string, jclass> NativeScriptRuntime::s_classCache;
15 changes: 0 additions & 15 deletions src/jni/NativeScriptRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,10 @@ namespace tns

static void AppFail(jthrowable throwable, const char *message);

static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args);

static void CreateGlobalCastFunctions(const v8::Local<v8::ObjectTemplate>& globalTemplate);

static std::vector<std::string> GetTypeMetadata(const std::string& name, int index);

static void AddApplicationModule(const std::string& appName, v8::Persistent<v8::Object>* applicationModule);

static jobjectArray GetMethodOverrides(JEnv& env, const v8::Local<v8::Object>& implementationObject);

static std::string LogExceptionStackTrace(const v8::TryCatch& tryCatch);
Expand All @@ -90,20 +86,13 @@ namespace tns

static void CreateTopLevelNamespaces(const v8::Local<v8::Object>& global);

static void CompileAndRun(std::string modulePath, bool& hasError, v8::Local<v8::Object>& moduleObj);

static v8::Local<v8::Object> FindClass(const std::string& className);

static MetadataTreeNode *metadataRoot;

static jclass PlatformClass;

static jclass RequireClass;

static jclass JAVA_LANG_STRING;

static std::string APP_FILES_DIR;

//

private:
Expand All @@ -125,8 +114,6 @@ namespace tns

static jmethodID APP_FAIL_METHOD_ID;

static jmethodID GET_MODULE_PATH_METHOD_ID;

static jmethodID GET_TYPE_METADATA;

static jmethodID ENABLE_VERBOSE_LOGGING_METHOD_ID;
Expand All @@ -135,8 +122,6 @@ namespace tns

static jmethodID GET_CHANGE_IN_BYTES_OF_USED_MEMORY_METHOD_ID;

static std::map<std::string, v8::Persistent<v8::Object>*> loadedModules;

static NumericCasts castFunctions;

static ArrayElementAccessor arrayElementAccessor;
Expand Down
Loading