00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #ifdef HAVE_JS
00037
00038 #include "import_script.h"
00039 #include "config_manager.h"
00040 #include "js_functions.h"
00041
00042 using namespace zmm;
00043
00044 ImportScript::ImportScript(Ref<Runtime> runtime) : Script(runtime)
00045 {
00046 String scriptPath = ConfigManager::getInstance()->getOption(CFG_IMPORT_SCRIPTING_IMPORT_SCRIPT);
00047
00048 #ifdef JS_THREADSAFE
00049 JS_SetContextThread(cx);
00050 JS_BeginRequest(cx);
00051 #endif
00052
00053 try
00054 {
00055 load(scriptPath);
00056 root = JS_NewScriptObject(cx, script);
00057 JS_AddNamedRoot(cx, &root, "ImportScript");
00058 }
00059 catch (Exception ex)
00060 {
00061 #ifdef JS_THREADSAFE
00062 JS_EndRequest(cx);
00063 JS_ClearContextThread(cx);
00064 #endif
00065 throw ex;
00066 }
00067 #ifdef JS_THREADSAFE
00068 JS_EndRequest(cx);
00069 JS_ClearContextThread(cx);
00070 #endif
00071 }
00072
00073 void ImportScript::processCdsObject(Ref<CdsObject> obj, String rootpath)
00074 {
00075 #ifdef JS_THREADSAFE
00076 JS_SetContextThread(cx);
00077 JS_BeginRequest(cx);
00078 #endif
00079 processed = obj;
00080 try
00081 {
00082 JSObject *orig = JS_NewObject(cx, NULL, NULL, glob);
00083 setObjectProperty(glob, _("orig"), orig);
00084 cdsObject2jsObject(obj, orig);
00085 setProperty(glob, _("object_root_path"), rootpath);
00086 execute();
00087 }
00088 catch (Exception ex)
00089 {
00090 processed = nil;
00091 #ifdef JS_THREADSAFE
00092 JS_EndRequest(cx);
00093 JS_ClearContextThread(cx);
00094 #endif
00095 throw ex;
00096 }
00097
00098 processed = nil;
00099
00100 gc_counter++;
00101 if (gc_counter > JS_CALL_GC_AFTER_NUM)
00102 {
00103 JS_MaybeGC(cx);
00104 gc_counter = 0;
00105 }
00106 #ifdef JS_THREADSAFE
00107 JS_EndRequest(cx);
00108 JS_ClearContextThread(cx);
00109 #endif
00110 }
00111
00112 ImportScript::~ImportScript()
00113 {
00114 #ifdef JS_THREADSAFE
00115 JS_SetContextThread(cx);
00116 JS_BeginRequest(cx);
00117 #endif
00118
00119 if (root)
00120 JS_RemoveRoot(cx, &root);
00121
00122 #ifdef JS_THREADSAFE
00123 JS_EndRequest(cx);
00124 JS_ClearContextThread(cx);
00125 #endif
00126
00127 }
00128
00129 #endif // HAVE_JS