1 // Copyright (c) 2012 Dennco Project
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // Created by tkawata on 12/15/2011.
20 #include "TKJSContainer.h"
22 #include "TKJSCellCode.h"
23 #include "TKJSBasicCell.h"
30 const std::string TKJSContainer::CELLTYPE_JSBASIC = "TKJSBasicCell";
31 const std::string TKJSContainer::CELLTYPE_UIBASE = "TKUIBaseCell";
33 static JSValueRef jsGlobalPrint(
38 const JSObjectRef args[],
41 static JSValueRef jsGlobalFQN(
46 const JSObjectRef args[],
49 TKJSContainer::TKJSContainer()
51 mContext = JSGlobalContextCreate(NULL);
52 mGlobalObject = JSContextGetGlobalObject(mContext);
54 JSStringRef jstrPrint = JSStringCreateWithUTF8CString("print");
55 JSObjectRef jfuncPrint = JSObjectMakeFunctionWithCallback(mContext, jstrPrint, (JSObjectCallAsFunctionCallback)jsGlobalPrint);
56 JSObjectSetProperty(mContext, mGlobalObject, jstrPrint, jfuncPrint, kJSPropertyAttributeNone, NULL);
57 JSStringRelease(jstrPrint);
59 JSStringRef jstrFQN = JSStringCreateWithUTF8CString("FQN");
60 JSObjectRef jfuncFQN = JSObjectMakeFunctionWithCallback(mContext, jstrFQN, (JSObjectCallAsFunctionCallback)jsGlobalFQN);
61 JSObjectSetProperty(mContext, mGlobalObject, jstrFQN, jfuncFQN, kJSPropertyAttributeNone, NULL);
62 JSStringRelease(jstrFQN);
64 JSStringRef jstrDGlobal = JSStringCreateWithUTF8CString("global");
65 mJGlobalObject = JSObjectMake(mContext, NULL, NULL);
66 JSObjectSetProperty(mContext, mGlobalObject, jstrDGlobal, mJGlobalObject, kJSPropertyAttributeNone, NULL);
67 JSStringRelease(jstrDGlobal);
69 JSStringRef jstrContainerVersion = JSStringCreateWithUTF8CString("container_version");
70 JSStringRef jstrContainerVersionValue = JSStringCreateWithUTF8CString("0.1");
71 JSValueRef jContainerVersionValue = JSValueMakeString(mContext, jstrContainerVersionValue);
72 JSObjectSetProperty(mContext, mJGlobalObject, jstrContainerVersion, jContainerVersionValue, kJSPropertyAttributeNone, NULL);
73 JSStringRelease(jstrContainerVersion);
74 JSStringRelease(jstrContainerVersionValue);
78 TKJSContainer::~TKJSContainer()
80 JSGlobalContextRelease(mContext);
84 TKCell* TKJSContainer::createCell(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string startupScript)
88 std::string type = cellCode->getCellAPIName();
90 if (type == CELLTYPE_JSBASIC)
92 TKJSBasicCell *basicCell = new TKJSBasicCell(this, theLocation, theName);
95 //do some init here if needed.
100 else if (type == CELLTYPE_UIBASE)
102 cell = new TKUICell(this, theLocation, theName);
106 TKLog::printf("ERROR in TKJSContainer::createCell. Type name:%s is not correct.", type.c_str());
112 std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str());
113 mCells.insert( std::map<std::string, TKCell*>::value_type(fqnName, cell));
114 cell->setCellCode(cellCode,(const void*)startupScript.c_str());
118 std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
119 TKLog::printf("Failed to create a cell. %s", fqnName.c_str());
126 TKCellCode* TKJSContainer::createCellCode(std::string theName, std::string theAPIType, std::string code)
128 TKJSCellCode *cellCode = new TKJSCellCode(theName, theAPIType, this, code);
132 mCellCodes.insert( std::map<std::string, TKCellCode*>::value_type(theName, cellCode));
138 void TKJSContainer::setValue(std::string key, float value)
140 JSStringRef jstr = JSStringCreateWithUTF8CString(key.c_str());
141 JSValueRef jvalue = JSValueMakeNumber(mContext, value);
142 JSObjectSetProperty(mContext, mJGlobalObject, jstr, jvalue, kJSPropertyAttributeNone, NULL);
143 JSStringRelease(jstr);
146 float TKJSContainer::getValue(std::string key)
148 JSStringRef jstr = JSStringCreateWithUTF8CString(key.c_str());
150 if (JSObjectHasProperty(mContext, mJGlobalObject, jstr))
152 JSValueRef jvalue = JSObjectGetProperty(mContext, mJGlobalObject, jstr, NULL);
153 JSStringRelease(jstr);
154 result = JSValueToNumber(mContext, jvalue,NULL);
159 bool TKJSContainer::doTick(float time)
161 for ( TKCellMap::iterator it = mCells.begin(); it != mCells.end(); ++it ) {
162 it->second->doTick(time);
168 JSValueRef jsGlobalPrint(
171 JSObjectRef jobjThis,
173 const JSObjectRef args[],
174 JSValueRef* jobjExp) {
177 JSStringRef jstrArg = JSValueToStringCopy(ctx, args[0], jobjExp);
178 size_t len = JSStringGetMaximumUTF8CStringSize(jstrArg);
179 char* szArg = (char*)malloc(len);
180 JSStringGetUTF8CString(jstrArg, szArg, len);
181 TKLog::printf("%s", szArg);
182 JSStringRelease(jstrArg);
186 return JSValueMakeUndefined(ctx);
189 JSValueRef jsGlobalFQN(
192 JSObjectRef jobjThis,
194 const JSObjectRef args[],
195 JSValueRef* jobjExp) {
197 JSValueRef jsResult = NULL;;
200 JSStringRef jstrLocation = JSValueToStringCopy(ctx, args[0], jobjExp);
201 JSStringRef jstrName = JSValueToStringCopy(ctx, args[1], jobjExp);
203 size_t locationLen = JSStringGetMaximumUTF8CStringSize(jstrLocation);
204 size_t nameLen = JSStringGetMaximumUTF8CStringSize(jstrName);
205 char* cLocation = (char*)malloc(locationLen);
206 char* cName = (char*)malloc(nameLen);
207 JSStringGetUTF8CString(jstrLocation, cLocation, locationLen);
208 JSStringGetUTF8CString(jstrName, cName, nameLen);
209 std::string fqnString = getFQNString(cLocation, cName);
211 jsResult = JSValueMakeString(ctx,JSStringCreateWithUTF8CString(fqnString.c_str()));
218 jsResult = JSValueMakeUndefined(ctx);