OSDN Git Service

Container builder and some refactoring. still working on.
[dennco/dennco.git] / Source / TKJSContainer.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
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.
7 //
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.
12 //
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/>.
15
16 //
17 //  Created by tkawata on 12/15/2011.
18 //
19
20 #include "TKJSContainer.h"
21 #include "TKCell.h"
22 #include "TKJSCellCode.h"
23 #include "TKJSBasicCell.h"
24 #include "TKUICell.h"
25 #include "TKLog.h"
26 #include "DNUtils.h"
27
28 #include <string>
29
30 const std::string TKJSContainer::CELLTYPE_JSBASIC = "TKJSBasicCell";
31 const std::string TKJSContainer::CELLTYPE_UIBASE = "TKUIBaseCell";
32
33 static JSValueRef jsGlobalPrint(
34                                                                 JSContextRef        ctx,
35                                                                 JSObjectRef         jobj,
36                                                                 JSObjectRef         jobjThis,
37                                                                 size_t              argLen,
38                                                                 const JSObjectRef   args[],
39                                                                 JSValueRef*         jobjExp);
40
41 static JSValueRef jsGlobalFQN(
42                                                                 JSContextRef        ctx,
43                                                                 JSObjectRef         jobj,
44                                                                 JSObjectRef         jobjThis,
45                                                                 size_t              argLen,
46                                                                 const JSObjectRef   args[],
47                                                                 JSValueRef*         jobjExp);
48
49 TKJSContainer::TKJSContainer()
50 {
51     mContext = JSGlobalContextCreate(NULL);
52     mGlobalObject = JSContextGetGlobalObject(mContext);
53
54     JSStringRef jstrPrint = JSStringCreateWithUTF8CString("print");
55     JSObjectRef jfuncPrint = JSObjectMakeFunctionWithCallback(mContext, jstrPrint, (JSObjectCallAsFunctionCallback)jsGlobalPrint);
56     JSObjectSetProperty(mContext, mGlobalObject, jstrPrint, jfuncPrint, kJSPropertyAttributeNone, NULL);
57     JSStringRelease(jstrPrint);
58
59     JSStringRef jstrFQN = JSStringCreateWithUTF8CString("FQN");
60     JSObjectRef jfuncFQN = JSObjectMakeFunctionWithCallback(mContext, jstrFQN, (JSObjectCallAsFunctionCallback)jsGlobalFQN);
61     JSObjectSetProperty(mContext, mGlobalObject, jstrFQN, jfuncFQN, kJSPropertyAttributeNone, NULL);
62     JSStringRelease(jstrFQN);
63
64     JSStringRef jstrDGlobal = JSStringCreateWithUTF8CString("global");
65     mJGlobalObject = JSObjectMake(mContext, NULL, NULL);
66     JSObjectSetProperty(mContext, mGlobalObject, jstrDGlobal, mJGlobalObject, kJSPropertyAttributeNone, NULL);
67     JSStringRelease(jstrDGlobal);
68
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);
75
76 }
77
78 TKJSContainer::~TKJSContainer()
79 {
80     JSGlobalContextRelease(mContext);
81     mContext = 0;
82 }
83
84 TKCell* TKJSContainer::createCell(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string startupScript)
85 {
86         TKCell *cell = NULL;
87         
88     std::string type = cellCode->getCellAPIName();
89     
90         if (type == CELLTYPE_JSBASIC)
91         {
92                 TKJSBasicCell *basicCell = new TKJSBasicCell(this, theLocation, theName);
93                 if (basicCell)
94                 {
95             //do some init here if needed.
96             
97                         cell = basicCell;
98                 }
99         }
100         else if (type == CELLTYPE_UIBASE)
101     {
102         cell = new TKUICell(this, theLocation, theName);
103     }
104     else
105         {
106                 TKLog::printf("ERROR in TKJSContainer::createCell. Type name:%s is not correct.", type.c_str());
107         }
108
109         
110         if (cell)
111         {
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());
115         }
116     else
117     {
118         std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str());
119         TKLog::printf("Failed to create a cell. %s", fqnName.c_str());
120     }
121     
122         return cell;
123
124 }
125
126 TKCellCode* TKJSContainer::createCellCode(std::string theName, std::string theAPIType, std::string code)
127 {
128     TKJSCellCode *cellCode = new TKJSCellCode(theName, theAPIType, this, code);
129     
130     if (cellCode)
131     {
132         mCellCodes.insert( std::map<std::string, TKCellCode*>::value_type(theName, cellCode));
133     }
134     return cellCode;
135 }
136
137
138 void TKJSContainer::setValue(std::string key, float value)
139 {    
140     JSStringRef jstr = JSStringCreateWithUTF8CString(key.c_str());
141     JSValueRef jvalue = JSValueMakeNumber(mContext, value);
142     JSObjectSetProperty(mContext, mJGlobalObject, jstr, jvalue, kJSPropertyAttributeNone, NULL);
143     JSStringRelease(jstr);
144 }
145
146 float TKJSContainer::getValue(std::string key)
147 {
148     JSStringRef jstr = JSStringCreateWithUTF8CString(key.c_str());
149     float result = 0.0;
150     if (JSObjectHasProperty(mContext, mJGlobalObject, jstr))
151     {
152         JSValueRef jvalue = JSObjectGetProperty(mContext, mJGlobalObject, jstr, NULL);
153         JSStringRelease(jstr);
154         result = JSValueToNumber(mContext, jvalue,NULL);
155     }
156     return result;
157 }
158
159 bool TKJSContainer::doTick(float time)
160 {
161         for ( TKCellMap::iterator it = mCells.begin(); it != mCells.end(); ++it ) {
162                 it->second->doTick(time);
163         }
164         
165         return true;
166 }
167
168 JSValueRef jsGlobalPrint(
169                                                                 JSContextRef        ctx,
170                                                                 JSObjectRef         jobj,
171                                                                 JSObjectRef         jobjThis,
172                                                                 size_t              argLen,
173                                                                 const JSObjectRef   args[],
174                                                                 JSValueRef*         jobjExp) {
175         
176     if (argLen) {
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);
183         free(szArg);
184     }
185         
186     return JSValueMakeUndefined(ctx);
187 }
188
189 JSValueRef jsGlobalFQN(
190                                                                 JSContextRef        ctx,
191                                                                 JSObjectRef         jobj,
192                                                                 JSObjectRef         jobjThis,
193                                                                 size_t              argLen,
194                                                                 const JSObjectRef   args[],
195                                                                 JSValueRef*         jobjExp) {
196         
197     JSValueRef jsResult = NULL;;
198     
199     if (argLen >= 2) {
200         JSStringRef     jstrLocation    = JSValueToStringCopy(ctx, args[0], jobjExp);
201         JSStringRef     jstrName        = JSValueToStringCopy(ctx, args[1], jobjExp);
202         
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);
210         
211         jsResult = JSValueMakeString(ctx,JSStringCreateWithUTF8CString(fqnString.c_str()));
212         
213         free(cLocation);
214         free(cName);
215     }
216     else
217     {
218         jsResult = JSValueMakeUndefined(ctx);        
219     }
220         
221     return jsResult;
222 }
223
224