@@ -26,7 +26,6 @@ using namespace v8;
26
26
using namespace std ;
27
27
using namespace tns ;
28
28
29
- // init
30
29
void NativeScriptRuntime::Init (JavaVM *jvm, ObjectManager *objectManager)
31
30
{
32
31
NativeScriptRuntime::jvm = jvm;
@@ -735,6 +734,103 @@ void NativeScriptRuntime::CreateGlobalCastFunctions(const Handle<ObjectTemplate>
735
734
}
736
735
737
736
737
+ void NativeScriptRuntime::CompileAndRun (string modulePath, bool & hasError, Handle<Object>& moduleObj, bool isBootstrapCall)
738
+ {
739
+ auto isolate = Isolate::GetCurrent ();
740
+
741
+ Local < Value > exportObj = Object::New (isolate);
742
+ auto tmpExportObj = new Persistent<Object>(isolate, exportObj.As <Object>());
743
+ loadedModules.insert (make_pair (modulePath, tmpExportObj));
744
+
745
+ TryCatch tc;
746
+
747
+ auto scriptText = Require::LoadModule (modulePath);
748
+
749
+ DEBUG_WRITE (" Compiling script (module %s)" , modulePath.c_str ());
750
+ Local < String > fullRequiredModulePath = ConvertToV8String (modulePath);
751
+ auto script = Script::Compile (scriptText, fullRequiredModulePath);
752
+ DEBUG_WRITE (" Compiled script (module %s)" , modulePath.c_str ());
753
+
754
+ if (ExceptionUtil::GetInstance ()->HandleTryCatch (tc, " Script " + modulePath + " contains compilation errors!" ))
755
+ {
756
+ loadedModules.erase (modulePath);
757
+ tmpExportObj->Reset ();
758
+ delete tmpExportObj;
759
+ hasError = true ;
760
+ }
761
+ else if (script.IsEmpty ())
762
+ {
763
+ // think about more descriptive message -> [script_name] was empty
764
+ DEBUG_WRITE (" %s was empty" , modulePath.c_str ());
765
+ }
766
+ else
767
+ {
768
+ DEBUG_WRITE (" Running script (module %s)" , modulePath.c_str ());
769
+
770
+ TryCatch tcRequire;
771
+
772
+ Local < Function > f = script->Run ().As <Function>();
773
+ if (ExceptionUtil::GetInstance ()->HandleTryCatch (tc))
774
+ {
775
+ DEBUG_WRITE (" Exception was handled in java code" );
776
+ }
777
+
778
+ // this is done so the initial bootstrap function is persistent (and to keep old logic)
779
+ if (isBootstrapCall) {
780
+ auto persistentAppModule = new Persistent<Object>(isolate, f);
781
+ }
782
+
783
+ auto result = f->Call (Object::New (isolate), 1 , &exportObj);
784
+ if (ExceptionUtil::GetInstance ()->HandleTryCatch (tc))
785
+ {
786
+ DEBUG_WRITE (" Exception was handled in java code" );
787
+ }
788
+ else
789
+ {
790
+ moduleObj = result.As <Object>();
791
+ }
792
+
793
+ // introducing isBootstrapCall in order to save the flow as it was (latter on we can think about including the following code for the bootstrap (initial) call
794
+ if (!isBootstrapCall) {
795
+ DEBUG_WRITE (" After Running script (module %s)" , modulePath.c_str ());
796
+
797
+ if (ExceptionUtil::GetInstance ()->HandleTryCatch (tcRequire))
798
+ {
799
+ loadedModules.erase (modulePath);
800
+ tmpExportObj->Reset ();
801
+ delete tmpExportObj;
802
+ hasError = true ;
803
+ tcRequire.ReThrow ();
804
+ }
805
+ else
806
+ {
807
+ if (moduleObj.IsEmpty ())
808
+ {
809
+ auto objectTemplate = ObjectTemplate::New ();
810
+ moduleObj = objectTemplate->NewInstance ();
811
+ }
812
+
813
+ DEBUG_WRITE (" Script completed (module %s)" , modulePath.c_str ());
814
+
815
+ if (!moduleObj->StrictEquals (exportObj))
816
+ {
817
+ loadedModules.erase (modulePath);
818
+ tmpExportObj->Reset ();
819
+ delete tmpExportObj;
820
+
821
+ auto persistentModuleObject = new Persistent<Object>(isolate, moduleObj.As <Object>());
822
+
823
+ loadedModules.insert (make_pair (modulePath, persistentModuleObject));
824
+ }
825
+ }
826
+ }
827
+ }
828
+ if (tc.HasCaught ())
829
+ {
830
+ tc.ReThrow ();
831
+ }
832
+ }
833
+
738
834
void NativeScriptRuntime::RequireCallback (const v8::FunctionCallbackInfo<v8::Value>& args)
739
835
{
740
836
SET_PROFILER_FRAME ();
@@ -782,67 +878,7 @@ void NativeScriptRuntime::RequireCallback(const v8::FunctionCallbackInfo<v8::Val
782
878
783
879
if (it == loadedModules.end ())
784
880
{
785
- Local<Value> exportObj = Object::New (isolate);
786
- auto tmpExportObj = new Persistent<Object>(isolate, exportObj.As <Object>());
787
- loadedModules.insert (make_pair (modulePath, tmpExportObj));
788
-
789
- TryCatch tc;
790
- auto scriptText = Require::LoadModule (modulePath);
791
-
792
- DEBUG_WRITE (" Compiling script (module %s)" , moduleName.c_str ());
793
- auto script = Script::Compile (scriptText, args[0 ].As <String>());
794
- DEBUG_WRITE (" Compiled script (module %s)" , moduleName.c_str ());
795
-
796
- if (ExceptionUtil::GetInstance ()->HandleTryCatch (tc)){
797
- loadedModules.erase (modulePath);
798
- tmpExportObj->Reset ();
799
- delete tmpExportObj;
800
- hasError = true ;
801
- }
802
- else {
803
- DEBUG_WRITE (" Running script (module %s)" , moduleName.c_str ());
804
-
805
- TryCatch tcRequire;
806
-
807
- Local<Function> f = script->Run ().As <Function>();
808
- auto result = f->Call (Object::New (isolate), 1 , &exportObj);
809
-
810
- moduleObj = result.As <Object>();
811
-
812
- DEBUG_WRITE (" After Running script (module %s)" , moduleName.c_str ());
813
-
814
- if (ExceptionUtil::GetInstance ()->HandleTryCatch (tcRequire)){
815
- loadedModules.erase (modulePath);
816
- tmpExportObj->Reset ();
817
- delete tmpExportObj;
818
- hasError = true ;
819
- tcRequire.ReThrow ();
820
- }
821
- else {
822
- if (moduleObj.IsEmpty ())
823
- {
824
- auto objectTemplate = ObjectTemplate::New ();
825
- moduleObj = objectTemplate->NewInstance ();
826
- }
827
-
828
- DEBUG_WRITE (" Script completed (module %s)" , moduleName.c_str ());
829
-
830
- if (!moduleObj->StrictEquals (exportObj))
831
- {
832
- loadedModules.erase (modulePath);
833
- tmpExportObj->Reset ();
834
- delete tmpExportObj;
835
-
836
- auto persistentModuleObject = new Persistent<Object>(isolate, moduleObj.As <Object>());
837
-
838
- loadedModules.insert (make_pair (modulePath, persistentModuleObject));
839
- }
840
- }
841
- }
842
- if (tc.HasCaught ())
843
- {
844
- tc.ReThrow ();
845
- }
881
+ CompileAndRun (modulePath, hasError, moduleObj, false /* is bootstrap call*/ );
846
882
}
847
883
else
848
884
{
0 commit comments