JSComponentFile.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. typescriptClass_(false)
  37. {
  38. }
  39. JSComponentFile::~JSComponentFile()
  40. {
  41. }
  42. void JSComponentFile::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<JSComponentFile>();
  45. }
  46. bool JSComponentFile::PushModule()
  47. {
  48. if (context_->GetEditorContext())
  49. return false;
  50. JSVM* vm = JSVM::GetJSVM(NULL);
  51. duk_context* ctx = vm->GetJSContext();
  52. const String& path = GetName();
  53. String pathName, fileName, ext;
  54. SplitPath(path, pathName, fileName, ext);
  55. if (path.Contains('/') || path.Contains('\\'))
  56. pathName += "/" + fileName;
  57. else
  58. pathName = fileName;
  59. duk_get_global_string(ctx, "require");
  60. duk_push_string(ctx, pathName.CString());
  61. if (duk_pcall(ctx, 1) != 0)
  62. {
  63. vm->SendJSErrorEvent();
  64. return false;
  65. }
  66. return true;
  67. }
  68. SharedPtr<JSComponent> JSComponentFile::CreateJSComponent()
  69. {
  70. SharedPtr<JSComponent> component;
  71. if (!scriptClass_)
  72. {
  73. component = new JSComponent(context_);
  74. }
  75. else
  76. {
  77. JSVM* vm = JSVM::GetJSVM(NULL);
  78. duk_context* ctx = vm->GetJSContext();
  79. PushModule();
  80. // Check to see if the module exposes a "default" constructor which is used by TS and ES2015
  81. duk_get_prop_string(ctx, -1, "default");
  82. if (!duk_is_function(ctx, -1))
  83. {
  84. // We are not a "default" style object, so reset the stack
  85. duk_pop(ctx);
  86. }
  87. duk_new(ctx, 0);
  88. if (duk_is_object(ctx, -1))
  89. {
  90. component = js_to_class_instance<JSComponent>(ctx, -1, 0);
  91. component->scriptClassInstance_ = true;
  92. }
  93. duk_pop(ctx);
  94. }
  95. if (component.Null())
  96. {
  97. ATOMIC_LOGERRORF("Failed to create script class from component file %s", GetName().CString());
  98. component = new JSComponent(context_);
  99. }
  100. component->SetComponentFile(this);
  101. return component;
  102. }
  103. bool JSComponentFile::InitModule()
  104. {
  105. if (context_->GetEditorContext())
  106. return true;
  107. JSVM* vm = JSVM::GetJSVM(NULL);
  108. duk_context* ctx = vm->GetJSContext();
  109. duk_idx_t top = duk_get_top(ctx);
  110. if (!PushModule())
  111. return false;
  112. if (duk_is_function(ctx, -1))
  113. {
  114. // constructor export
  115. scriptClass_ = true;
  116. }
  117. else if (duk_is_object(ctx, -1))
  118. {
  119. // Look for "default" constructor which is used by TypeScript and ES2015
  120. duk_get_prop_string(ctx, -1, "default");
  121. if (!duk_is_function(ctx, -1))
  122. {
  123. duk_pop(ctx);
  124. duk_get_prop_string(ctx, -1, "component");
  125. if (!duk_is_function(ctx, -1))
  126. {
  127. ATOMIC_LOGERRORF("Component file export object does not export a key \"component\" function: %s", GetName().CString());
  128. duk_set_top(ctx, top);
  129. return false;
  130. }
  131. scriptClass_ = false;
  132. } else {
  133. // this is a TS or ES2015 default class
  134. scriptClass_ = true;
  135. }
  136. }
  137. duk_set_top(ctx, top);
  138. return true;
  139. }
  140. bool JSComponentFile::BeginLoad(Deserializer& source)
  141. {
  142. typescriptClass_ = false;
  143. if (!InitModule())
  144. return false;
  145. // TODO: move the inspector field parsing to the JavascriptImporter
  146. // which will work with obfusication, minimization, or bytecode dumps
  147. unsigned dataSize = source.GetSize();
  148. if (!dataSize && !source.GetName().Empty())
  149. {
  150. ATOMIC_LOGERROR("Zero sized component file in " + source.GetName());
  151. return false;
  152. }
  153. SharedArrayPtr<char> buffer(new char[dataSize + 1]);
  154. if (source.Read(buffer.Get(), dataSize) != dataSize)
  155. return false;
  156. buffer[dataSize] = '\0';
  157. String text = buffer.Get();
  158. text.Replace("\r", "");
  159. Vector<String> lines = text.Split('\n');
  160. String eval;
  161. bool valid = false;
  162. int leftBracketCount = 0;
  163. int rightBracketCount = 0;
  164. for (unsigned i = 0; i < lines.Size(); i++)
  165. {
  166. String line = lines[i];
  167. // TODO: better TS detection
  168. if (line.Contains("function __() { this.constructor = d; }"))
  169. typescriptClass_ = true;
  170. bool added = false;
  171. if (!eval.Length())
  172. {
  173. line = line.Trimmed();
  174. if (line.StartsWith("inspectorFields"))
  175. {
  176. added = true;
  177. eval = line + "\n";
  178. }
  179. else if (line.StartsWith("this.inspectorFields"))
  180. {
  181. added = true;
  182. eval = line.Substring(5) + "\n";
  183. }
  184. else if (line.StartsWith("_this.inspectorFields"))
  185. {
  186. added = true;
  187. eval = line.Substring(6) + "\n";
  188. }
  189. else if (line.StartsWith("var inspectorFields"))
  190. {
  191. added = true;
  192. eval = line.Substring(4) + "\n";
  193. }
  194. }
  195. else
  196. {
  197. added = true;
  198. eval += line + "\n";
  199. }
  200. if (added) {
  201. for (unsigned j = 0; j < line.Length(); j++)
  202. {
  203. if (line.At(j) == '{')
  204. leftBracketCount++;
  205. else if (line.At(j) == '}')
  206. rightBracketCount++;
  207. }
  208. }
  209. if (eval.Length() && leftBracketCount && leftBracketCount == rightBracketCount)
  210. {
  211. valid = true;
  212. break;
  213. }
  214. }
  215. if (valid)
  216. {
  217. JSVM* vm = JSVM::GetJSVM(NULL);
  218. if (!vm)
  219. return true;
  220. duk_context* ctx = vm->GetJSContext();
  221. int top = duk_get_top(ctx);
  222. duk_push_string(ctx, eval.CString());
  223. duk_push_string(ctx, "eval");
  224. if (duk_pcompile(ctx, DUK_COMPILE_EVAL) != 0)
  225. {
  226. // couldn't eval the inspector fields
  227. duk_set_top(ctx, top);
  228. return true;
  229. }
  230. else if (duk_is_function(ctx, -1) && duk_pcall(ctx, 0) == DUK_EXEC_SUCCESS)
  231. {
  232. if (duk_is_object(ctx, -1))
  233. {
  234. duk_enum(ctx, -1, DUK_ENUM_OWN_PROPERTIES_ONLY);
  235. while (duk_next(ctx, -1, 1)) {
  236. String name = duk_get_string(ctx, -2);
  237. VariantType variantType = VAR_NONE;
  238. Variant defaultValue;
  239. if (duk_is_string(ctx, -1))
  240. {
  241. variantType = VAR_STRING;
  242. defaultValue = duk_to_string(ctx, -1);
  243. }
  244. else if (duk_is_number(ctx, -1))
  245. {
  246. variantType = VAR_FLOAT;
  247. defaultValue = (float) duk_to_number(ctx, -1);
  248. }
  249. else if (duk_is_array(ctx, -1))
  250. {
  251. int length = duk_get_length(ctx, -1);
  252. if (length > 0)
  253. {
  254. duk_get_prop_index(ctx, -1, 0);
  255. // resource ref detection
  256. if (duk_is_string(ctx, -1))
  257. {
  258. const char* classname = duk_to_string(ctx, -1);
  259. duk_pop(ctx);
  260. const char* name = NULL;
  261. if (length > 1)
  262. {
  263. duk_get_prop_index(ctx, -1, 1);
  264. name = duk_require_string(ctx, -1);
  265. duk_pop(ctx);
  266. }
  267. ResourceRef resourceRef(classname);
  268. if (name)
  269. resourceRef.name_ = name;
  270. variantType = VAR_RESOURCEREF;
  271. defaultValue = resourceRef;
  272. }
  273. else
  274. {
  275. variantType = (VariantType) ((int)duk_require_number(ctx, -1));
  276. duk_pop(ctx);
  277. // detect int enum
  278. if (length > 1 && (variantType == VAR_INT || variantType == VAR_FLOAT
  279. || variantType == VAR_DOUBLE))
  280. {
  281. duk_get_prop_index(ctx, -1, 1);
  282. if (duk_is_number(ctx, -1))
  283. {
  284. js_to_variant(ctx, -1, defaultValue);
  285. }
  286. else if (duk_is_array(ctx, -1))
  287. {
  288. int enumLength = duk_get_length(ctx, -1);
  289. for (unsigned i = 0; i < enumLength; i++)
  290. {
  291. duk_get_prop_index(ctx, -1, i);
  292. String enumName = duk_require_string(ctx, -1);
  293. AddEnum(name, EnumInfo(enumName, int(i)));
  294. duk_pop(ctx);
  295. }
  296. }
  297. duk_pop(ctx);
  298. if (length > 2)
  299. {
  300. duk_get_prop_index(ctx, -1, 2);
  301. // default value
  302. js_to_variant(ctx, -1, defaultValue);
  303. duk_pop(ctx);
  304. }
  305. }
  306. else if (length > 1)
  307. {
  308. duk_get_prop_index(ctx, -1, 1);
  309. // default value
  310. js_to_variant(ctx, -1, defaultValue);
  311. duk_pop(ctx);
  312. }
  313. }
  314. }
  315. }
  316. else if (duk_is_boolean(ctx, -1))
  317. {
  318. variantType = VAR_BOOL;
  319. defaultValue = duk_to_boolean(ctx, -1) ? true : false;
  320. }
  321. if (defaultValue.GetType() != VAR_NONE)
  322. {
  323. AddDefaultValue(name, defaultValue);
  324. }
  325. if (variantType != VAR_NONE)
  326. {
  327. AddField(name, variantType);
  328. }
  329. duk_pop_2(ctx); // pop key value
  330. }
  331. duk_pop(ctx); // pop enum object
  332. }
  333. }
  334. duk_set_top(ctx, top);
  335. }
  336. SetMemoryUse(0);
  337. return true;
  338. }
  339. bool JSComponentFile::Save(Serializer& dest) const
  340. {
  341. return true;
  342. }
  343. }