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;
2928void 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-
761750void 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-
914756vector<string> NativeScriptRuntime::GetTypeMetadata (const string& name, int index)
915757{
916758 JEnv env;
@@ -1108,23 +950,19 @@ int NativeScriptRuntime::GetArrayLength(const Local<Object>& arr)
1108950
1109951JavaVM* NativeScriptRuntime::jvm = nullptr ;
1110952jclass NativeScriptRuntime::PlatformClass = nullptr ;
1111- jclass NativeScriptRuntime::RequireClass = nullptr ;
1112953jclass NativeScriptRuntime::JAVA_LANG_STRING = nullptr ;
1113954jmethodID NativeScriptRuntime::RESOLVE_CLASS_METHOD_ID = nullptr ;
1114955jmethodID NativeScriptRuntime::CREATE_INSTANCE_METHOD_ID = nullptr ;
1115956jmethodID NativeScriptRuntime::CACHE_CONSTRUCTOR_METHOD_ID = nullptr ;
1116957jmethodID NativeScriptRuntime::APP_FAIL_METHOD_ID = nullptr ;
1117- jmethodID NativeScriptRuntime::GET_MODULE_PATH_METHOD_ID = nullptr ;
1118958jmethodID NativeScriptRuntime::GET_TYPE_METADATA = nullptr ;
1119959jmethodID NativeScriptRuntime::ENABLE_VERBOSE_LOGGING_METHOD_ID = nullptr ;
1120960jmethodID NativeScriptRuntime::DISABLE_VERBOSE_LOGGING_METHOD_ID = nullptr ;
1121961jmethodID NativeScriptRuntime::GET_CHANGE_IN_BYTES_OF_USED_MEMORY_METHOD_ID = nullptr ;
1122- map<string, Persistent<Object>*> NativeScriptRuntime::loadedModules;
1123962MetadataTreeNode* NativeScriptRuntime::metadataRoot = nullptr ;
1124963ObjectManager* NativeScriptRuntime::objectManager = nullptr ;
1125964NumericCasts NativeScriptRuntime::castFunctions;
1126965ArrayElementAccessor NativeScriptRuntime::arrayElementAccessor;
1127966FieldAccessor NativeScriptRuntime::fieldAccessor;
1128- string NativeScriptRuntime::APP_FILES_DIR;
1129967map<string, int > NativeScriptRuntime::s_constructorCache;
1130968map<std::string, jclass> NativeScriptRuntime::s_classCache;
0 commit comments