JSComponentFile.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Core/Context.h>
  23. #include <Atomic/IO/Deserializer.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/Core/Profiler.h>
  26. #include <Atomic/Resource/ResourceCache.h>
  27. #include <Atomic/IO/Serializer.h>
  28. #include "JSComponentFile.h"
  29. #include "JSComponent.h"
  30. #include "JSVM.h"
  31. namespace Atomic
  32. {
  33. JSComponentFile::JSComponentFile(Context* context) :
  34. ScriptComponentFile(context),
  35. scriptClass_(false)
  36. {
  37. }
  38. JSComponentFile::~JSComponentFile()
  39. {
  40. }
  41. void JSComponentFile::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<JSComponentFile>();
  44. }
  45. bool JSComponentFile::PushModule()
  46. {
  47. if (context_->GetEditorContext())
  48. return false;
  49. JSVM* vm = JSVM::GetJSVM(NULL);
  50. duk_context* ctx = vm->GetJSContext();
  51. const String& path = GetName();
  52. String pathName, fileName, ext;
  53. SplitPath(path, pathName, fileName, ext);
  54. if (path.Contains('/') || path.Contains('\\'))
  55. pathName += "/" + fileName;
  56. else
  57. pathName = fileName;
  58. duk_get_global_string(ctx, "require");
  59. duk_push_string(ctx, pathName.CString());
  60. if (duk_pcall(ctx, 1) != 0)
  61. {
  62. vm->SendJSErrorEvent();
  63. return false;
  64. }
  65. return true;
  66. }
  67. SharedPtr<JSComponent> JSComponentFile::CreateJSComponent()
  68. {
  69. SharedPtr<JSComponent> component;
  70. if (!scriptClass_)
  71. {
  72. component = new JSComponent(context_);
  73. }
  74. else
  75. {
  76. JSVM* vm = JSVM::GetJSVM(NULL);
  77. duk_context* ctx = vm->GetJSContext();
  78. PushModule();
  79. // Check to see if the module exposes a "default" constructor which is used by TS and ES2015
  80. duk_get_prop_string(ctx, -1, "default");
  81. if (!duk_is_function(ctx, -1))
  82. {
  83. // We are not a "default" style object, so reset the stack
  84. duk_pop(ctx);
  85. }
  86. duk_new(ctx, 0);
  87. if (duk_is_object(ctx, -1))
  88. {
  89. component = js_to_class_instance<JSComponent>(ctx, -1, 0);
  90. component->scriptClassInstance_ = true;
  91. }
  92. duk_pop(ctx);
  93. }
  94. if (component.Null())
  95. {
  96. ATOMIC_LOGERRORF("Failed to create script class from component file %s", GetName().CString());
  97. component = new JSComponent(context_);
  98. }
  99. component->SetComponentFile(this);
  100. return component;
  101. }
  102. bool JSComponentFile::InitModule()
  103. {
  104. if (context_->GetEditorContext())
  105. return true;
  106. JSVM* vm = JSVM::GetJSVM(NULL);
  107. duk_context* ctx = vm->GetJSContext();
  108. duk_idx_t top = duk_get_top(ctx);
  109. if (!PushModule())
  110. return false;
  111. if (duk_is_function(ctx, -1))
  112. {
  113. // constructor export
  114. scriptClass_ = true;
  115. }
  116. else if (duk_is_object(ctx, -1))
  117. {
  118. // Look for "default" constructor which is used by TypeScript and ES2015
  119. duk_get_prop_string(ctx, -1, "default");
  120. if (!duk_is_function(ctx, -1))
  121. {
  122. duk_pop(ctx);
  123. duk_get_prop_string(ctx, -1, "component");
  124. if (!duk_is_function(ctx, -1))
  125. {
  126. ATOMIC_LOGERRORF("Component file export object does not export a key \"component\" function: %s", GetName().CString());
  127. duk_set_top(ctx, top);
  128. return false;
  129. }
  130. scriptClass_ = false;
  131. } else {
  132. // this is a TS or ES2015 default class
  133. scriptClass_ = true;
  134. }
  135. }
  136. duk_set_top(ctx, top);
  137. return true;
  138. }
  139. bool JSComponentFile::BeginLoad(Deserializer& source)
  140. {
  141. if (!InitModule())
  142. return false;
  143. // TODO: move the inspector field parsing to the JavascriptImporter
  144. // which will work with obfusication, minimization, or bytecode dumps
  145. unsigned dataSize = source.GetSize();
  146. if (!dataSize && !source.GetName().Empty())
  147. {
  148. ATOMIC_LOGERROR("Zero sized component file in " + source.GetName());
  149. return false;
  150. }
  151. SharedArrayPtr<char> buffer(new char[dataSize + 1]);
  152. if (source.Read(buffer.Get(), dataSize) != dataSize)
  153. return false;
  154. buffer[dataSize] = '\0';
  155. String text = buffer.Get();
  156. text.Replace("\r", "");
  157. Vector<String> lines = text.Split('\n');
  158. String eval;
  159. bool valid = false;
  160. int leftBracketCount = 0;
  161. int rightBracketCount = 0;
  162. for (unsigned i = 0; i < lines.Size(); i++)
  163. {
  164. String line = lines[i];
  165. bool added = false;
  166. if (!eval.Length())
  167. {
  168. line = line.Trimmed();
  169. if (line.StartsWith("inspectorFields"))
  170. {
  171. added = true;
  172. eval = line + "\n";
  173. }
  174. else if (line.StartsWith("this.inspectorFields"))
  175. {
  176. added = true;
  177. eval = line.Substring(5) + "\n";
  178. }
  179. else if (line.StartsWith("_this.inspectorFields"))
  180. {
  181. added = true;
  182. eval = line.Substring(6) + "\n";
  183. }
  184. else if (line.StartsWith("var inspectorFields"))
  185. {
  186. added = true;
  187. eval = line.Substring(4) + "\n";
  188. }
  189. }
  190. else
  191. {
  192. added = true;
  193. eval += line + "\n";
  194. }
  195. if (added) {
  196. for (unsigned j = 0; j < line.Length(); j++)
  197. {
  198. if (line.At(j) == '{')
  199. leftBracketCount++;
  200. else if (line.At(j) == '}')
  201. rightBracketCount++;
  202. }
  203. }
  204. if (eval.Length() && leftBracketCount && leftBracketCount == rightBracketCount)
  205. {
  206. valid = true;
  207. break;
  208. }
  209. }
  210. if (valid)
  211. {
  212. JSVM* vm = JSVM::GetJSVM(NULL);
  213. if (!vm)
  214. return true;
  215. duk_context* ctx = vm->GetJSContext();
  216. int top = duk_get_top(ctx);
  217. duk_push_string(ctx, eval.CString());
  218. duk_push_string(ctx, "eval");
  219. if (duk_pcompile(ctx, DUK_COMPILE_EVAL) != 0)
  220. {
  221. // couldn't eval the inspector fields
  222. duk_set_top(ctx, top);
  223. return true;
  224. }
  225. else if (duk_is_function(ctx, -1) && duk_pcall(ctx, 0) == DUK_EXEC_SUCCESS)
  226. {
  227. if (duk_is_object(ctx, -1))
  228. {
  229. duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY);
  230. while (duk_next(ctx, -1, 1)) {
  231. String name = duk_get_string(ctx, -2);
  232. VariantType variantType = VAR_NONE;
  233. Variant defaultValue;
  234. if (duk_is_string(ctx, -1))
  235. {
  236. variantType = VAR_STRING;
  237. defaultValue = duk_to_string(ctx, -1);
  238. }
  239. else if (duk_is_number(ctx, -1))
  240. {
  241. variantType = VAR_FLOAT;
  242. defaultValue = (float) duk_to_number(ctx, -1);
  243. }
  244. else if (duk_is_array(ctx, -1))
  245. {
  246. int length = duk_get_length(ctx, -1);
  247. if (length > 0)
  248. {
  249. duk_get_prop_index(ctx, -1, 0);
  250. // resource ref detection
  251. if (duk_is_string(ctx, -1))
  252. {
  253. const char* classname = duk_to_string(ctx, -1);
  254. duk_pop(ctx);
  255. const char* name = NULL;
  256. if (length > 1)
  257. {
  258. duk_get_prop_index(ctx, -1, 1);
  259. name = duk_require_string(ctx, -1);
  260. duk_pop(ctx);
  261. }
  262. ResourceRef resourceRef(classname);
  263. if (name)
  264. resourceRef.name_ = name;
  265. variantType = VAR_RESOURCEREF;
  266. defaultValue = resourceRef;
  267. }
  268. else
  269. {
  270. variantType = (VariantType) ((int)duk_require_number(ctx, -1));
  271. duk_pop(ctx);
  272. // detect int enum
  273. if (length > 1 && (variantType == VAR_INT || variantType == VAR_FLOAT
  274. || variantType == VAR_DOUBLE))
  275. {
  276. duk_get_prop_index(ctx, -1, 1);
  277. if (duk_is_number(ctx, -1))
  278. {
  279. js_to_variant(ctx, -1, defaultValue);
  280. }
  281. else if (duk_is_array(ctx, -1))
  282. {
  283. int enumLength = duk_get_length(ctx, -1);
  284. for (unsigned i = 0; i < enumLength; i++)
  285. {
  286. duk_get_prop_index(ctx, -1, i);
  287. String enumName = duk_require_string(ctx, -1);
  288. AddEnum(name, EnumInfo(enumName, Variant(float(i))));
  289. duk_pop(ctx);
  290. }
  291. }
  292. duk_pop(ctx);
  293. if (length > 2)
  294. {
  295. duk_get_prop_index(ctx, -1, 2);
  296. // default value
  297. js_to_variant(ctx, -1, defaultValue);
  298. duk_pop(ctx);
  299. }
  300. }
  301. else if (length > 1)
  302. {
  303. duk_get_prop_index(ctx, -1, 1);
  304. // default value
  305. js_to_variant(ctx, -1, defaultValue);
  306. duk_pop(ctx);
  307. }
  308. }
  309. }
  310. }
  311. else if (duk_is_boolean(ctx, -1))
  312. {
  313. variantType = VAR_BOOL;
  314. defaultValue = duk_to_boolean(ctx, -1) ? true : false;
  315. }
  316. if (defaultValue.GetType() != VAR_NONE)
  317. {
  318. AddDefaultValue(name, defaultValue);
  319. }
  320. if (variantType != VAR_NONE)
  321. {
  322. AddField(name, variantType, false);
  323. }
  324. duk_pop_2(ctx); // pop key value
  325. }
  326. duk_pop(ctx); // pop enum object
  327. }
  328. }
  329. duk_set_top(ctx, top);
  330. }
  331. SetMemoryUse(0);
  332. return true;
  333. }
  334. bool JSComponentFile::Save(Serializer& dest) const
  335. {
  336. return true;
  337. }
  338. }