Skip to content

Commit d13c79b

Browse files
committed
Merge pull request #258 from NativeScript/gatanasov/code-cache
Gatanasov/code cache
2 parents 4153ee3 + bf4c82e commit d13c79b

16 files changed

+360
-246
lines changed

src/jni/Android.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ LOCAL_SRC_FILES := com_tns_AssetExtractor.cpp com_tns_Platform.cpp com_tns_JsDeb
7272
FieldAccessor.cpp ArrayElementAccessor.cpp \
7373
ExceptionUtil.cpp Util.cpp Logger.cpp Profiler.cpp \
7474
ObjectManager.cpp NumericCasts.cpp WeakRef.cpp \
75-
MetadataMethodInfo.cpp SimpleProfiler.cpp JType.cpp File.cpp Require.cpp
75+
MetadataMethodInfo.cpp SimpleProfiler.cpp JType.cpp File.cpp Require.cpp Constants.cpp
7676
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
7777
LOCAL_LDLIBS := -llog -landroid -lz
7878
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)

src/jni/ArgConverter.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,6 @@ namespace tns
3737

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

40-
static jlong ObjectToLong(jobject object);
41-
42-
static jboolean ObjectToBoolean(jobject object);
43-
44-
static jchar ObjectToChar(jobject object);
45-
46-
static jbyte ObjectToByte(jobject object);
47-
48-
static jshort ObjectToShort(jobject object);
49-
50-
static jint ObjectToInt(jobject object);
51-
52-
static jfloat ObjectToFloat(jobject object);
53-
54-
static jdouble ObjectToDouble(jobject object);
55-
5640
static jstring ObjectToString(jobject object);
5741

5842
static v8::Local<v8::String> jcharToV8String(jchar value);

src/jni/Constants.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Constants.cpp
3+
*
4+
* Created on: Nov 6, 2015
5+
* Author: gatanasov
6+
*/
7+
8+
#include "Constants.h"
9+
10+
std::string Constants::APP_ROOT_FOLDER_PATH = "";
11+
bool Constants::V8_CACHE_COMPILED_CODE = false;
12+
std::string Constants::V8_STARTUP_FLAGS = "";

src/jni/Constants.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef CONSTANTS_H_
22
#define CONSTANTS_H_
33

4+
#include <string>
5+
46
class Constants
57
{
68
public:
@@ -12,6 +14,10 @@ class Constants
1214

1315
static std::string APP_ROOT_FOLDER_PATH;
1416

17+
static std::string V8_STARTUP_FLAGS;
18+
19+
static bool V8_CACHE_COMPILED_CODE;
20+
1521
private:
1622
Constants() {}
1723
};

src/jni/File.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77

88
#include "File.h"
99
#include <sstream>
10+
#include <fstream>
1011

1112
using namespace std;
1213

1314
namespace tns
1415
{
16+
bool File::Exists(const string& path)
17+
{
18+
std::ifstream infile(path.c_str());
19+
return infile.good();
20+
}
1521
string File::ReadText(const string& filePath)
1622
{
1723
int len;

src/jni/File.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace tns
1717
public:
1818
static const char* ReadText(const std::string& filePath, int& length, bool& isNew);
1919
static std::string ReadText(const std::string& filePath);
20+
static bool Exists(const std::string& filePath);
2021
private:
2122
static const int BUFFER_SIZE = 1024 * 1024;
2223
static char* Buffer;

src/jni/JniLocalRef.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "JniLocalRef.h"
2+
#include "JType.h"
23
#include <cassert>
34

45
using namespace v8;
@@ -70,6 +71,12 @@ JniLocalRef::operator jclass() const
7071
return (jclass)m_obj;
7172
}
7273

74+
JniLocalRef::operator jboolean() const
75+
{
76+
JEnv env;
77+
return JType::BooleanValue(env, m_obj);
78+
}
79+
7380

7481
JniLocalRef::operator jthrowable() const
7582
{

src/jni/JniLocalRef.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace tns
2525

2626
operator jobject() const;
2727

28+
operator jboolean() const;
29+
2830
operator jclass() const;
2931

3032
operator jstring() const;

src/jni/NativeScriptRuntime.cpp

Lines changed: 1 addition & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "JsArgToArrayConverter.h"
1010
#include "ArgConverter.h"
1111
#include "v8-profiler.h"
12-
#include "Constants.h"
1312
#include <assert.h>
1413
#include <iostream>
1514
#include <sstream>
@@ -29,6 +28,7 @@ using namespace tns;
2928
void NativeScriptRuntime::Init(JavaVM *jvm, ObjectManager *objectManager)
3029
{
3130
NativeScriptRuntime::jvm = jvm;
31+
Require::Init();
3232

3333
JEnv env;
3434

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

41-
RequireClass = env.FindClass("com/tns/Require");
42-
assert(RequireClass != nullptr);
43-
4441
RESOLVE_CLASS_METHOD_ID = env.GetStaticMethodID(PlatformClass, "resolveClass", "(Ljava/lang/String;[Ljava/lang/String;)Ljava/lang/Class;");
4542
assert(RESOLVE_CLASS_METHOD_ID != nullptr);
4643

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

59-
GET_MODULE_PATH_METHOD_ID = env.GetStaticMethodID(RequireClass, "getModulePath", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
60-
assert(GET_MODULE_PATH_METHOD_ID != nullptr);
61-
6256
ENABLE_VERBOSE_LOGGING_METHOD_ID = env.GetStaticMethodID(PlatformClass, "enableVerboseLogging", "()V");
6357
assert(ENABLE_VERBOSE_LOGGING_METHOD_ID != nullptr);
6458

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

755749

756-
void NativeScriptRuntime::AddApplicationModule(const string& appName, v8::Persistent<v8::Object>* applicationModule)
757-
{
758-
loadedModules.insert(make_pair(appName, applicationModule));
759-
}
760-
761750
void NativeScriptRuntime::CreateGlobalCastFunctions(const Local<ObjectTemplate>& globalTemplate)
762751
{
763752
castFunctions.CreateGlobalCastFunctions(globalTemplate);
764753
}
765754

766755

767-
void NativeScriptRuntime::CompileAndRun(string modulePath, bool& hasError, Local<Object>& moduleObj)
768-
{
769-
auto isolate = Isolate::GetCurrent();
770-
771-
Local<Value> exportObj = Object::New(isolate);
772-
auto tmpExportObj = new Persistent<Object>(isolate, exportObj.As<Object>());
773-
loadedModules.insert(make_pair(modulePath, tmpExportObj));
774-
775-
TryCatch tc;
776-
777-
auto scriptText = Require::LoadModule(modulePath);
778-
779-
DEBUG_WRITE("Compiling script (module %s)", modulePath.c_str());
780-
auto fullRequiredModulePath = ConvertToV8String(modulePath);
781-
auto script = Script::Compile(scriptText, fullRequiredModulePath);
782-
DEBUG_WRITE("Compiled script (module %s)", modulePath.c_str());
783-
784-
if (ExceptionUtil::GetInstance()->HandleTryCatch(tc, "Script " + modulePath + " contains compilation errors!"))
785-
{
786-
hasError = true;
787-
}
788-
else if (script.IsEmpty())
789-
{
790-
//think about more descriptive message -> [script_name] was empty
791-
DEBUG_WRITE("%s was empty", modulePath.c_str());
792-
}
793-
else
794-
{
795-
DEBUG_WRITE("Running script (module %s)", modulePath.c_str());
796-
797-
auto f = script->Run().As<Function>();
798-
if (ExceptionUtil::GetInstance()->HandleTryCatch(tc, "Error running script " + modulePath))
799-
{
800-
hasError = true;
801-
}
802-
else
803-
{
804-
auto result = f->Call(Object::New(isolate), 1, &exportObj);
805-
if(ExceptionUtil::GetInstance()->HandleTryCatch(tc, "Error calling module function "))
806-
{
807-
hasError = true;
808-
}
809-
else
810-
{
811-
moduleObj = result.As<Object>();
812-
if (moduleObj.IsEmpty())
813-
{
814-
auto objectTemplate = ObjectTemplate::New();
815-
moduleObj = objectTemplate->NewInstance();
816-
}
817-
818-
DEBUG_WRITE("Script completed (module %s)", modulePath.c_str());
819-
820-
if (!moduleObj->StrictEquals(exportObj))
821-
{
822-
loadedModules.erase(modulePath);
823-
tmpExportObj->Reset();
824-
delete tmpExportObj;
825-
826-
auto persistentModuleObject = new Persistent<Object>(isolate, moduleObj.As<Object>());
827-
828-
loadedModules.insert(make_pair(modulePath, persistentModuleObject));
829-
}
830-
}
831-
}
832-
}
833-
834-
if(hasError)
835-
{
836-
loadedModules.erase(modulePath);
837-
tmpExportObj->Reset();
838-
delete tmpExportObj;
839-
840-
// this handles recursive require calls
841-
tc.ReThrow();
842-
}
843-
}
844-
845-
void NativeScriptRuntime::RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args)
846-
{
847-
SET_PROFILER_FRAME();
848-
849-
auto isolate = Isolate::GetCurrent();
850-
851-
if (args.Length() != 2)
852-
{
853-
isolate->ThrowException(ConvertToV8String("require should be called with two parameters"));
854-
return;
855-
}
856-
if (!args[0]->IsString())
857-
{
858-
isolate->ThrowException(ConvertToV8String("require's first parameter should be string"));
859-
return;
860-
}
861-
if (!args[1]->IsString())
862-
{
863-
isolate->ThrowException(ConvertToV8String("require's second parameter should be string"));
864-
return;
865-
}
866-
867-
string moduleName = ConvertToString(args[0].As<String>());
868-
string callingModuleDirName = ConvertToString(args[1].As<String>());
869-
870-
JEnv env;
871-
JniLocalRef jsModulename(env.NewStringUTF(moduleName.c_str()));
872-
JniLocalRef jsCallingModuleDirName(env.NewStringUTF(callingModuleDirName.c_str()));
873-
JniLocalRef jsModulePath(env.CallStaticObjectMethod(RequireClass, GET_MODULE_PATH_METHOD_ID, (jstring) jsModulename, (jstring) jsCallingModuleDirName));
874-
875-
// cache the required modules by full path, not name only, since there might be some collisions with relative paths and names
876-
string modulePath = ArgConverter::jstringToString((jstring) jsModulePath);
877-
if(modulePath == ""){
878-
// module not found
879-
stringstream ss;
880-
ss << "Module \"" << moduleName << "\" not found";
881-
string exception = ss.str();
882-
ExceptionUtil::GetInstance()->ThrowExceptionToJs(exception);
883-
return;
884-
}
885-
if (modulePath == "EXTERNAL_FILE_ERROR")
886-
{
887-
// module not found
888-
stringstream ss;
889-
ss << "Module \"" << moduleName << "\" is located on the external storage. Modules can be private application files ONLY";
890-
string exception = ss.str();
891-
ExceptionUtil::GetInstance()->ThrowExceptionToJs(exception);
892-
return;
893-
}
894-
895-
auto it = loadedModules.find(modulePath);
896-
897-
Local<Object> moduleObj;
898-
bool hasError = false;
899-
900-
if (it == loadedModules.end())
901-
{
902-
CompileAndRun(modulePath, hasError, moduleObj);
903-
}
904-
else
905-
{
906-
moduleObj = Local<Object>::New(isolate, *((*it).second));
907-
}
908-
909-
if(!hasError){
910-
args.GetReturnValue().Set(moduleObj);
911-
}
912-
}
913-
914756
vector<string> NativeScriptRuntime::GetTypeMetadata(const string& name, int index)
915757
{
916758
JEnv env;
@@ -1108,23 +950,19 @@ int NativeScriptRuntime::GetArrayLength(const Local<Object>& arr)
1108950

1109951
JavaVM* NativeScriptRuntime::jvm = nullptr;
1110952
jclass NativeScriptRuntime::PlatformClass = nullptr;
1111-
jclass NativeScriptRuntime::RequireClass = nullptr;
1112953
jclass NativeScriptRuntime::JAVA_LANG_STRING = nullptr;
1113954
jmethodID NativeScriptRuntime::RESOLVE_CLASS_METHOD_ID = nullptr;
1114955
jmethodID NativeScriptRuntime::CREATE_INSTANCE_METHOD_ID = nullptr;
1115956
jmethodID NativeScriptRuntime::CACHE_CONSTRUCTOR_METHOD_ID = nullptr;
1116957
jmethodID NativeScriptRuntime::APP_FAIL_METHOD_ID = nullptr;
1117-
jmethodID NativeScriptRuntime::GET_MODULE_PATH_METHOD_ID = nullptr;
1118958
jmethodID NativeScriptRuntime::GET_TYPE_METADATA = nullptr;
1119959
jmethodID NativeScriptRuntime::ENABLE_VERBOSE_LOGGING_METHOD_ID = nullptr;
1120960
jmethodID NativeScriptRuntime::DISABLE_VERBOSE_LOGGING_METHOD_ID = nullptr;
1121961
jmethodID NativeScriptRuntime::GET_CHANGE_IN_BYTES_OF_USED_MEMORY_METHOD_ID = nullptr;
1122-
map<string, Persistent<Object>*> NativeScriptRuntime::loadedModules;
1123962
MetadataTreeNode* NativeScriptRuntime::metadataRoot = nullptr;
1124963
ObjectManager* NativeScriptRuntime::objectManager = nullptr;
1125964
NumericCasts NativeScriptRuntime::castFunctions;
1126965
ArrayElementAccessor NativeScriptRuntime::arrayElementAccessor;
1127966
FieldAccessor NativeScriptRuntime::fieldAccessor;
1128-
string NativeScriptRuntime::APP_FILES_DIR;
1129967
map<string, int> NativeScriptRuntime::s_constructorCache;
1130968
map<std::string, jclass> NativeScriptRuntime::s_classCache;

src/jni/NativeScriptRuntime.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,10 @@ namespace tns
7070

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

73-
static void RequireCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
74-
7573
static void CreateGlobalCastFunctions(const v8::Local<v8::ObjectTemplate>& globalTemplate);
7674

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

79-
static void AddApplicationModule(const std::string& appName, v8::Persistent<v8::Object>* applicationModule);
80-
8177
static jobjectArray GetMethodOverrides(JEnv& env, const v8::Local<v8::Object>& implementationObject);
8278

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

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

93-
static void CompileAndRun(std::string modulePath, bool& hasError, v8::Local<v8::Object>& moduleObj);
94-
9589
static v8::Local<v8::Object> FindClass(const std::string& className);
9690

9791
static MetadataTreeNode *metadataRoot;
9892

9993
static jclass PlatformClass;
10094

101-
static jclass RequireClass;
102-
10395
static jclass JAVA_LANG_STRING;
104-
105-
static std::string APP_FILES_DIR;
106-
10796
//
10897

10998
private:
@@ -125,8 +114,6 @@ namespace tns
125114

126115
static jmethodID APP_FAIL_METHOD_ID;
127116

128-
static jmethodID GET_MODULE_PATH_METHOD_ID;
129-
130117
static jmethodID GET_TYPE_METADATA;
131118

132119
static jmethodID ENABLE_VERBOSE_LOGGING_METHOD_ID;
@@ -135,8 +122,6 @@ namespace tns
135122

136123
static jmethodID GET_CHANGE_IN_BYTES_OF_USED_MEMORY_METHOD_ID;
137124

138-
static std::map<std::string, v8::Persistent<v8::Object>*> loadedModules;
139-
140125
static NumericCasts castFunctions;
141126

142127
static ArrayElementAccessor arrayElementAccessor;

0 commit comments

Comments
 (0)