00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00026 #include <archon/x3d/server/field_type.H>
00027 #include <archon/x3d/server/field.H>
00028 #include <archon/x3d/server/server.H>
00029 #include <archon/x3d/server/script.H>
00030 #include <archon/x3d/server/script_ecma.H>
00031
00032 namespace Archon
00033 {
00034 namespace X3D
00035 {
00036 const NodeType *ScriptNode::type = 0;
00037 const NodeType *Script::type = 0;
00038
00039 void initializeScriptComponent()
00040 {
00041 vector<const FieldBase *> fields;
00042
00043
00044
00045
00046 ScriptNode::type =
00047 NodeType::newAbstract("ScriptNode", false, 0,
00048 ChildNode::type);
00049
00050
00051
00052
00053 fields.push_back(newPrivateField("directOutput", SFBool::type,
00054 &Script::directOutput));
00055 fields.push_back(newPrivateField("mustEvaluate", SFBool::type,
00056 &Script::mustEvaluate));
00057 Script::type =
00058 NodeType::newConcrete("Script", "children",
00059 Script::instantiate, &fields,
00060 ScriptNode::type, UrlObject::type);
00061 fields.clear();
00062 }
00063
00064
00065 Script::Script(BackRef<ExecutionContext> c):
00066 NodeBase(c), directOutput(false), mustEvaluate(false)
00067 {
00068 }
00069
00070
00075 void Script::update()
00076 {
00077 Ref<const Loader::Contents> c = getContents();
00078 if(!c) return;
00079 Mutex::Lock l(context->server->scenegraphMutex);
00080 if(engine) return;
00081 const Loader::ScriptContents *d =
00082 dynamic_cast<const Loader::ScriptContents *>(c.get());
00083 if(!d) ARCHON_THROW1(InternalException,
00084 "Script::update: Got non-script "
00085 "contents object");
00086 if(d->language == "ecmascript")
00087 engine = new ECMA::Engine(this, d->sourceCode, d->sourceName, d->sourceLine);
00088 else ARCHON_THROW1(InternalException,
00089 "Script::update: Got unexpected language");
00090 }
00091
00092 void Script::clear()
00093 {
00094 engine = 0;
00095 }
00096
00101 void Script::onLoaded()
00102 {
00103 if(!isRealized()) return;
00104 update();
00105 }
00106
00107 void Script::onRealized()
00108 {
00109 update();
00110 }
00111
00112 void Script::handleEvent(int fieldIndex, Time timestamp)
00113 {
00114 if(engine) engine->handleEvent(fieldIndex, timestamp);
00115 }
00116
00117 void Script::handleCDATA(string s, Time stamp)
00118 {
00119
00120 if(!Text::isPrefixOf("ecmascript:", s, true) &&
00121 !Text::isPrefixOf("javascript:", s, true))
00122 s = "ecmascript:" + s;
00123 addUri(s, stamp);
00124 }
00125 }
00126 }