You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JSParser.cpp 126KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783
  1. #include "JSParser.h"
  2. #include "../../../tools/StringUtils.h"
  3. #include <iostream>
  4. #include <fstream>
  5. #include <algorithm>
  6. extern std::ofstream assignfile;
  7. extern std::ofstream execfile;
  8. std::string typeOfStorage(js_internal_storage *storage) {
  9. if (!storage) {
  10. std::cout << "null passed into typeOfStorage\n";
  11. return "Corrupt";
  12. }
  13. js_string *string = dynamic_cast<js_string*>(storage);
  14. if (string) return "string";
  15. js_number *number = dynamic_cast<js_number*>(storage);
  16. if (number) return "number";
  17. js_bool *jbool = dynamic_cast<js_bool*>(storage);
  18. if (jbool) return "bool";
  19. js_function *func = dynamic_cast<js_function*>(storage);
  20. if (func) return "func";
  21. js_array *arr = dynamic_cast<js_array*>(storage);
  22. if (arr) return "array";
  23. js_object *obj = dynamic_cast<js_object*>(storage);
  24. if (obj) return "object";
  25. js_reference *ref = dynamic_cast<js_reference*>(storage);
  26. if (ref) return "reference";
  27. js_forward *fwd = dynamic_cast<js_forward*>(storage);
  28. if (fwd) return "forward";
  29. return "unknown";
  30. }
  31. bool jsIsFalse(js_internal_storage *generic) {
  32. std::string type = typeOfStorage(generic);
  33. if (type == "string") {
  34. js_string *string = dynamic_cast<js_string*>(generic);
  35. return string->value == "" || string->value == "0";
  36. } else
  37. if (type == "number") {
  38. js_number *jnum = dynamic_cast<js_number*>(generic);
  39. return !jnum->value;
  40. } else
  41. if (type == "bool") {
  42. js_bool *jb = dynamic_cast<js_bool*>(generic);
  43. return !jb->value;
  44. } else
  45. //if (type == "unknown") {
  46. // why is this needed?
  47. //return true;
  48. //} else
  49. if (type == "Corrupt") {
  50. return true;
  51. } else {
  52. std::cout << "unknown type [" << type << "]\n";
  53. }
  54. return false;
  55. }
  56. js_internal_storage *jsLocateKey(const js_function *scope, const std::string key) {
  57. // do we have it?
  58. auto locate = scope->locals.value.find(key);
  59. if (locate == scope->locals.value.end()) {
  60. // we do have a parent?
  61. if (scope->parentScope) {
  62. return jsLocateKey(scope->parentScope, key);
  63. }
  64. // no parent
  65. return nullptr;
  66. }
  67. // we have it
  68. return locate->second;
  69. }
  70. std::string jsLocatePtrKey(const js_function *scope, js_internal_storage *val) {
  71. // do we have it?
  72. std::string sourceKey = "";
  73. for(auto it2 : scope->locals.value) {
  74. std::cout << "ptr[" << val << "]==[" << it2.second << "] root[" << scope << "]\n";
  75. if (it2.second == val) {
  76. return it2.first;
  77. break;
  78. }
  79. }
  80. // we do have a parent?
  81. if (scope->parentScope) {
  82. return jsLocatePtrKey(scope->parentScope, val);
  83. }
  84. // no parent
  85. return "";
  86. }
  87. void jsDisplayScope(const js_function *scope, size_t level) {
  88. std::cout << "There are " << scope->locals.value.size() << " elements at this level " << level << "\n";
  89. for(auto it : scope->locals.value) {
  90. std::string type = typeOfStorage(it.second);
  91. if (type == "string") {
  92. js_string *string = dynamic_cast<js_string*>(it.second);
  93. std::cout << "[" << level << "]" << "[" << it.first << "] stringValue[" << string->value << "] address[" << it.second << "]\n";
  94. } else {
  95. std::cout << "[" << level << "]" << "[" << it.first << "] type[" << type << "] address[" << it.second << "]\n";
  96. }
  97. }
  98. if (scope->parentScope) {
  99. level++;
  100. jsDisplayScope(scope->parentScope, level);
  101. }
  102. }
  103. // if a function don't pass a starting or ending {}
  104. std::vector<std::string> jsGetTokens(const std::string &source, const size_t start) {
  105. std::vector<std::string> tokens;
  106. //std::cout << "jsGetTokens start [" << source.substr(start) << "]\n";
  107. // tokenize it
  108. size_t cursor;
  109. unsigned char state = 0;
  110. size_t last = start?start - 1 : 0;
  111. size_t quoteStart = 0;
  112. size_t scopeLevel = 0;
  113. size_t jsonStart = 0;
  114. size_t jsonLevel = 0;
  115. size_t parenLevel = 0;
  116. size_t parenStart = 0;
  117. size_t functionStart = 0;
  118. for (cursor = start; cursor < source.length(); cursor++) {
  119. //std::cout << "jsGetTokens step [" << cursor << "] char[" << source[cursor] << "] state[" << std::to_string(state) << "]\n";
  120. if (state == 0) {
  121. if (source[cursor] == '{') {
  122. state = 1; // JSON
  123. jsonStart = cursor;
  124. jsonLevel++;
  125. //std::cout << "Entering JSON: " << cursor << std::endl;
  126. } else if (source[cursor] == '(') {
  127. state = 8; // in a function call or prototype
  128. parenStart = cursor;
  129. parenLevel++;
  130. } else if (source[cursor] == '\'') { // quotes just for allowing [;{}\n] in quotes
  131. quoteStart = cursor;
  132. state = 4;
  133. } else if (source[cursor] == '"') {
  134. quoteStart = cursor;
  135. state = 5;
  136. } else if (source[cursor] == '/' && source.length() > cursor + 1 && source[cursor + 1] == '/') {
  137. // single line comment
  138. state = 2;
  139. } else if (source[cursor] == '/' && source.length() > cursor + 1 && source[cursor + 1] == '*') {
  140. // Multiline comment
  141. state = 3;
  142. } else if (source[cursor] == '/') {
  143. // regex
  144. if (source.length() > cursor + 1) {
  145. size_t endex = locateRegexEnd(source, cursor + 1); // +1 because it can't start on /
  146. cursor = endex; // put here after regex
  147. // state = 9;
  148. } else {
  149. std::cout << "jsGetTokens - warning - no characters left in state 0\n";
  150. }
  151. } else if (source[cursor] == 'v' && source.length() > cursor + 3 && source[cursor + 1] == 'a'
  152. && source[cursor + 2] == 'r' && source[cursor + 3] == ' ') {
  153. // var
  154. state = 7;
  155. } else if (source[cursor] == 'f' && source.length() > cursor + 8 && source[cursor + 1] == 'u'
  156. && source[cursor + 2] == 'n' && source[cursor + 3] == 'c' && source[cursor + 4] == 't'
  157. && source[cursor + 5] == 'i' && source[cursor + 6] == 'o' && source[cursor + 7] == 'n') {
  158. //std::cout << "Entering function: " << cursor << std::endl;
  159. state = 6;
  160. functionStart = cursor;
  161. }
  162. } else if (state == 1) {
  163. // inside a scope (JSON)
  164. if (source[cursor] == '{') {
  165. jsonLevel++;
  166. } else if (source[cursor] == '}') {
  167. jsonLevel--;
  168. if (!jsonLevel) {
  169. //std::cout << "Exiting JSON: " << source.substr(jsonStart, cursor - jsonStart) << "\n" << std::endl;
  170. state = 0; // exit JSON
  171. }
  172. }
  173. } else if (state == 8) {
  174. // inside a paren (function)
  175. //std::cout << "looking at [" << source[cursor] << "]@" << cursor << std::endl;
  176. if (source[cursor] == '(') {
  177. parenLevel++;
  178. } else if (source[cursor] == ')') {
  179. parenLevel--;
  180. if (!parenLevel) {
  181. //std::cout << "Exiting Paren: " << source.substr(parenStart, cursor - parenStart) << "\n" << std::endl;
  182. state = 0; // exit JSON
  183. }
  184. }
  185. } else if (state == 2) {
  186. // inside a single line comment
  187. if (source[cursor] == '\n') {
  188. last = cursor;
  189. state = 0;
  190. }
  191. } else if (state == 3) {
  192. // inside a multiline comment
  193. if (source[cursor] == '*' && source.length() > cursor + 1 && source[cursor + 1] == '/') {
  194. // end multiline comment
  195. last = cursor;
  196. state = 0;
  197. }
  198. } else if (state == 4) {
  199. // inside single quote
  200. if (source[cursor] == '\'') {
  201. if (source[cursor - 1] != '\\') {
  202. //std::string quote = source.substr(quoteStart + 1, cursor - quoteStart - 1);
  203. //std::cout << "single quote: " << quote << std::endl;
  204. state = 0;
  205. }
  206. }
  207. } else if (state == 5) {
  208. // inside double quote
  209. if (source[cursor] == '"') {
  210. if (source[cursor - 1] != '\\') {
  211. //std::string quote = source.substr(quoteStart + 1, cursor - quoteStart - 1);
  212. //std::cout << "double quote: " << quote << std::endl;
  213. state = 0;
  214. }
  215. }
  216. } else if (state == 7) {
  217. }
  218. //
  219. if (source[cursor] == '{') {
  220. scopeLevel++;
  221. }
  222. bool endIt = false;
  223. if (source[cursor] == '}') {
  224. scopeLevel--;
  225. if (state == 6 && !scopeLevel) {
  226. //std::cout << "Exiting function: " << source.substr(functionStart, cursor - functionStart) << "\n" << std::endl;
  227. state = 0;
  228. endIt = true;
  229. }
  230. }
  231. // state 0 or 7, ignore states 1-6
  232. if ((state == 0 || state == 7) && !scopeLevel) {
  233. if (source[cursor] == '\n' || source[cursor] == ';' || endIt || (source[cursor] == ',' && state != 7)) {
  234. // FIXME: ; in for loops
  235. std::string token = source.substr(last ? last + 1 : last, last ? (cursor - last - 1) : cursor );
  236. if (source[cursor] == '}') {
  237. token += '}';
  238. }
  239. // scopeLevel[" << scopeLevel << "]"
  240. //std::cout << "got token [" << token << "] ending[" << source[cursor] << "] endIt[" << endIt << "]" << std::endl;
  241. if (token.length()<3) {
  242. //std::cout << "token too short [" << token << "]" << std::endl;
  243. } else {
  244. tokens.push_back(token);
  245. }
  246. last = cursor;
  247. if (state == 7) { // allow var constructs to end normally and take us out of var construct
  248. state = 0; // reset state
  249. }
  250. }
  251. }
  252. }
  253. std::string token = source.substr(last ? last + 1 : last, last ? (cursor - last - 1) : cursor );
  254. //&& !token.length() // all look like complete valid tokens
  255. if (!state ) {
  256. if (token.length()) {
  257. // we can't just be discarding stuff
  258. tokens.push_back(token);
  259. }
  260. return tokens;
  261. }
  262. std::cout << "jsGetTokens - out of characters in state " << std::to_string(state) << " token[" << token << "]" << std::endl;
  263. std::cout << "got token [" << token << "] ending[" << source[cursor] << "]" << std::endl;
  264. if (token.length()<3) {
  265. std::cout << "token too short [" << token << "]" << std::endl;
  266. } else {
  267. tokens.push_back(token);
  268. }
  269. return tokens;
  270. }
  271. js_internal_storage *doFunctionCall(js_function *func, std::string params, js_function &scope) {
  272. if (func == nullptr) {
  273. std::cout << "passed null into doFunctionCall\n";
  274. return nullptr;
  275. }
  276. return jsParseTokens(func->tokens, &scope);
  277. }
  278. // this should evaluate an expression and return it's return value
  279. int doExpressionLevel = 0;
  280. js_internal_storage *doExpression(js_function &rootScope, std::string token) {
  281. std::vector<std::string> expression_parts;
  282. size_t cursor;
  283. size_t last = 0;
  284. size_t quoteStart = 0;
  285. size_t parenStart = 0;
  286. size_t parenLevel = 0;
  287. size_t trinaryLevel = 0;
  288. unsigned char state = 0;
  289. std::cout << "doExpression start[" << token << "]\n";
  290. // parse expression
  291. for (cursor = 0; cursor < token.length(); cursor++) {
  292. std::cout << "doExpression parse phase state[" << std::to_string(state) << "] char[" << token[cursor] << "]\n";
  293. if (state == 0) {
  294. // ||
  295. // &&
  296. // <, >, <=, >=, ==, ===, !=, !==
  297. // +, -
  298. // *, /, %, !
  299. // ?, >>, <<
  300. if (token[cursor] == 'f' && token.length() > cursor + 7 && token[cursor + 1] == 'u'
  301. && token[cursor + 2] == 'n' && token[cursor + 3] == 'c' && token[cursor + 4] == 't'
  302. && token[cursor + 5] == 'i' && token[cursor + 6] == 'o' && token[cursor + 7] == 'n') {
  303. state = 3;
  304. }
  305. if (token[cursor] == '\'') {
  306. quoteStart = cursor;
  307. state = 4;
  308. } else
  309. if (token[cursor] == '"') {
  310. quoteStart = cursor;
  311. state = 5;
  312. } else
  313. if (token[cursor] == '(') {
  314. parenStart = cursor;
  315. parenLevel++;
  316. state = 8;
  317. if (last != cursor) {
  318. // could be a func call or func def (w/ or w/o call)
  319. expression_parts.push_back(token.substr(last, cursor - last));
  320. }
  321. last = cursor + 1;
  322. expression_parts.push_back("(");
  323. } else
  324. if (token[cursor] == '{') {
  325. parenStart = cursor;
  326. parenLevel++;
  327. state = 1;
  328. //std::cout << "last " << last << " cursor " << cursor << std::endl;
  329. if (last != cursor) {
  330. expression_parts.push_back(token.substr(last, cursor - last - 1));
  331. }
  332. last = cursor + 1;
  333. expression_parts.push_back("{");
  334. }
  335. // single =
  336. if (token[cursor] == '=' && token.length() > cursor + 1 && token[cursor + 1] != '=') {
  337. //state = 1;
  338. //std::cout << "starting = at " << cursor << " last: " << last << std::endl;
  339. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  340. expression_parts.push_back("=");
  341. }
  342. // hack for JSON parsing
  343. if (token[cursor] == ':') {
  344. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  345. expression_parts.push_back("=");
  346. state = 2;
  347. }
  348. // ||
  349. if (token[cursor] == '|' && token.length() > cursor + 1 && token[cursor + 1] == '|') {
  350. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  351. expression_parts.push_back("||");
  352. }
  353. if (token[cursor] == '&' && token.length() > cursor + 1 && token[cursor + 1] == '&') {
  354. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 2;
  355. expression_parts.push_back("&&");
  356. }
  357. if (token[cursor] == '>' && token.length() > cursor + 1 && token[cursor + 1] != '=' && token[cursor + 1] != '>') {
  358. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  359. expression_parts.push_back(">");
  360. }
  361. if (token[cursor] == '<' && token.length() > cursor + 1 && token[cursor + 1] != '=' && token[cursor + 1] != '<') {
  362. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  363. expression_parts.push_back("<");
  364. }
  365. if (token[cursor] == '<' && token.length() > cursor + 1 && token[cursor + 1] == '&') {
  366. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  367. expression_parts.push_back("<=");
  368. }
  369. if (token[cursor] == '>' && token.length() > cursor + 1 && token[cursor + 1] == '&') {
  370. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  371. expression_parts.push_back(">=");
  372. }
  373. if (token[cursor] == '=' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] != '=') {
  374. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  375. expression_parts.push_back("==");
  376. }
  377. if (token[cursor] == '=' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] == '=') {
  378. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  379. expression_parts.push_back("===");
  380. }
  381. if (token[cursor] == '!' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] != '=') {
  382. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  383. expression_parts.push_back("!=");
  384. }
  385. if (token[cursor] == '!' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] == '=') {
  386. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  387. expression_parts.push_back("!==");
  388. }
  389. // +
  390. if (token[cursor] == '+') {
  391. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  392. expression_parts.push_back("+");
  393. } else
  394. if (token[cursor] == '-') {
  395. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  396. expression_parts.push_back("+");
  397. } else
  398. if (token[cursor] == '*') {
  399. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  400. expression_parts.push_back("*");
  401. } else
  402. if (token[cursor] == '/') {
  403. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  404. expression_parts.push_back("/");
  405. } else
  406. if (token[cursor] == '%') {
  407. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  408. expression_parts.push_back("%");
  409. } else
  410. if (token[cursor] == '!') {
  411. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  412. expression_parts.push_back("!");
  413. } else
  414. if (token[cursor] == '?') {
  415. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  416. expression_parts.push_back("?");
  417. trinaryLevel++;
  418. state = 9;
  419. }
  420. } else if (state == 1) {
  421. if (token[cursor] == '{') {
  422. parenLevel++;
  423. } else
  424. if (token[cursor] == '}') {
  425. parenLevel--;
  426. if (!parenLevel) {
  427. expression_parts.push_back(token.substr(last, cursor - last));
  428. last = cursor + 1;
  429. expression_parts.push_back("}");
  430. state = 0;
  431. }
  432. }
  433. } else if (state == 2) { // json state (can be moved)
  434. if (token[cursor] == ',') {
  435. expression_parts.push_back(token.substr(last, cursor - last));
  436. last = cursor + 1;
  437. state = 0;
  438. }
  439. } else if (state == 3) { // function start (can be moved)
  440. if (token[cursor] == '{') {
  441. // lets put the function prototype
  442. expression_parts.push_back(token.substr(last, cursor - last));
  443. last = cursor + 1;
  444. parenLevel++;
  445. state = 6; // move to the function body
  446. }
  447. } else if (state == 4) {
  448. if (token[cursor] == '\'') {
  449. if (token[cursor - 1] != '\\') {
  450. //std::string quote = token.substr(quoteStart + 1, cursor - quoteStart - 1);
  451. //expression_parts.push_back(quote);
  452. //std::cout << "single quote: " << quote << std::endl;
  453. state = 0;
  454. }
  455. }
  456. } else if (state == 5) {
  457. if (token[cursor] == '"') {
  458. if (token[cursor - 1] != '\\') {
  459. //std::string quote = token.substr(quoteStart + 1, cursor - quoteStart - 1);
  460. //expression_parts.push_back(quote);
  461. //std::cout << "double quote: " << quote << std::endl;
  462. state = 0;
  463. }
  464. }
  465. } else if (state == 6) {
  466. if (token[cursor] == '{') {
  467. parenLevel++;
  468. } else
  469. if (token[cursor] == '}') {
  470. parenLevel--;
  471. if (!parenLevel) {
  472. expression_parts.push_back(token.substr(last, cursor - last));
  473. last = cursor + 1;
  474. expression_parts.push_back("}"); // end function
  475. state = 0;
  476. }
  477. }
  478. } else if (state == 8) {
  479. if (token[cursor] == '(') {
  480. parenLevel++;
  481. } else
  482. if (token[cursor] == ')') {
  483. parenLevel--;
  484. if (!parenLevel) {
  485. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  486. expression_parts.push_back(")");
  487. state = 0;
  488. }
  489. }
  490. } else if (state == 9) {
  491. if (token[cursor] == '?') {
  492. trinaryLevel++;
  493. } else
  494. if (token[cursor] == ':') {
  495. trinaryLevel--;
  496. if (!trinaryLevel) {
  497. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  498. expression_parts.push_back(":");
  499. state = 0;
  500. }
  501. }
  502. }
  503. }
  504. // flush last part
  505. std::cout << "doExpression end state[" << std::to_string(state) << "] cursor[" << cursor << "] last[" << last << "]\n";
  506. if (cursor != last) {
  507. std::cout << "pushing last part\n";
  508. expression_parts.push_back(token.substr(last, cursor - last));
  509. }
  510. // execute expression
  511. std::cout << "doExpression has [" << expression_parts.size() << "]parts\n";
  512. js_internal_storage *stack = nullptr;
  513. state = 0;
  514. std::string callFuncName = "";
  515. js_function *callFunc = nullptr;
  516. std::string params = "";
  517. //js_function *func = nullptr;
  518. size_t i = 0;
  519. for(auto it : expression_parts) {
  520. std::string trimmedToken = trim(it);
  521. i++;
  522. if (trimmedToken == "") continue;
  523. std::cout << "doExpression part[" << (i - 1) << "][" << it << "] state[" << std::to_string(state) << "]\n";
  524. if (state == 0) {
  525. // &&
  526. if (trimmedToken == "&&") {
  527. // if stack is false abort
  528. std::cout << "typeOf stack is [" << typeOfStorage(stack) << "]\n";
  529. std::string type = typeOfStorage(stack);
  530. if (type=="string") {
  531. js_string *string = dynamic_cast<js_string*>(stack);
  532. std::cout << "stack points to [" << stack << "] value[" << string->value << "]\n";
  533. if (string!=nullptr) {
  534. if (string->value == "" || string->value == "0") {
  535. std::cout << "I believe this value to be false [" << string->value << "]\n";
  536. return nullptr;
  537. } else {
  538. std::cout << "I believe this value to be not false [" << string->value << "]\n";
  539. }
  540. } else {
  541. std::cout << "couldnt convert string\n";
  542. }
  543. } else if (type == "number") {
  544. js_number *jnum = dynamic_cast<js_number*>(stack);
  545. std::cout << "stack points to [" << stack << "] value[" << jnum->value << "]\n";
  546. if (jnum!=nullptr) {
  547. if (!jnum->value) {
  548. std::cout << "I believe this value to be false [" << jnum->value << "]\n";
  549. return nullptr;
  550. } else {
  551. std::cout << "I believe this value to be not false [" << jnum->value << "]\n";
  552. }
  553. } else {
  554. std::cout << "couldnt convert string\n";
  555. }
  556. } else {
  557. std::cout << "doExpression, I don't know how to process this type - WRITE ME\n";
  558. }
  559. continue;
  560. }
  561. if (trimmedToken == "!") {
  562. state = 4;
  563. continue;
  564. }
  565. if (trimmedToken == "{") {
  566. // parseJSON
  567. state = 5;
  568. continue;
  569. }
  570. std::string first8 = trimmedToken.substr(0, 8);
  571. if (first8 == "function") {
  572. std::string prototype = trimmedToken.substr(9, trimmedToken.length() - 10);
  573. std::cout << "extracted[" << prototype << "]\n";
  574. js_function *jsfunc = new js_function;
  575. jsfunc->parameters = split(prototype, ',');
  576. stack = jsfunc;
  577. continue;
  578. }
  579. // function name
  580. // (
  581. if (trimmedToken == "(") {
  582. // function call
  583. std::cout << "func call param start\n";
  584. state = 2;
  585. continue;
  586. }
  587. // function params
  588. // )
  589. if (trimmedToken == ")") {
  590. std::cout << "func call param end\n";
  591. state = 3;
  592. continue;
  593. }
  594. if (trimmedToken == "}") {
  595. // {return document.getElementById(e)}
  596. /*
  597. doExpression has [3]parts
  598. doExpression part[{] state[0]
  599. doExpression part[return document.getElementById(e)] state[5]
  600. */
  601. // ?
  602. continue;
  603. }
  604. // variable
  605. // object.property
  606. js_internal_storage *dereferenceTest = dereferenceObject(trimmedToken, &rootScope);
  607. if (dereferenceTest) {
  608. if (typeOfStorage(dereferenceTest)=="func") {
  609. // it could be a call...
  610. callFuncName = it;
  611. callFunc = dynamic_cast<js_function *>(dereferenceTest);
  612. if (!callFunc) {
  613. std::cout << "Couldnt cast deference to func\n";
  614. }
  615. state = 1;
  616. } else {
  617. stack = dereferenceTest;
  618. }
  619. continue;
  620. }
  621. // it's a string constant
  622. if ((trimmedToken[0] == '"' && trimmedToken[trimmedToken.length() - 1] == '"') ||
  623. (trimmedToken[0] == '\'' && trimmedToken[trimmedToken.length() - 1] == '\'')) {
  624. js_string *jstring = new js_string;
  625. jstring->value = trimmedToken.substr(1, -1);
  626. stack = jstring;
  627. continue;
  628. }
  629. if (trimmedToken == "true") {
  630. js_bool *jbool = new js_bool;
  631. jbool->value = true;
  632. stack = jbool;
  633. continue;
  634. } else if (trimmedToken == "null") {
  635. // FIXME: is null a type?
  636. js_bool *jbool = new js_bool;
  637. jbool->value = false;
  638. stack = jbool;
  639. continue;
  640. } else if (trimmedToken == "false") {
  641. js_bool *jbool = new js_bool;
  642. jbool->value = false;
  643. stack = jbool;
  644. continue;
  645. }
  646. // number constant
  647. bool allDigits = true;
  648. for(auto it2 : trimmedToken) {
  649. if (it2 >= '0' && it2 <= '9') {
  650. } else {
  651. if (it2 != '.') {
  652. allDigits = false;
  653. }
  654. }
  655. }
  656. if (allDigits) {
  657. js_number *jnum = new js_number;
  658. jnum->value = std::stoi(trimmedToken);
  659. std::cout << "allDigits value[" << trimmedToken << "] => jnum[" << jnum->value << "]\n";
  660. stack = jnum;
  661. continue;
  662. }
  663. js_internal_storage **isVar = getObjectKeyPointer(it, &rootScope);
  664. if (isVar != nullptr) {
  665. std::cout << "isVar [" << isVar << "]\n";
  666. std::cout << "stack could be [" << *isVar << "] type[" << typeOfStorage(stack) << "]\n";
  667. if (typeOfStorage(stack)=="func") {
  668. callFuncName = it;
  669. callFunc = dynamic_cast<js_function *>(dereferenceTest);
  670. state = 1;
  671. // (
  672. // 1
  673. // )
  674. } else {
  675. stack = *isVar; // set stack to point to this variable
  676. }
  677. /*
  678. if (typeOfStorage(stack)=="string") {
  679. js_string *string = dynamic_cast<js_string*>(stack);
  680. std::cout << "string stack value[" << string->value << "]\n";
  681. }
  682. */
  683. } else {
  684. js_internal_storage *scopeTest = jsLocateKey(&rootScope, it);
  685. if (scopeTest) {
  686. stack = scopeTest;
  687. } else {
  688. jsDisplayScope(&rootScope, 0);
  689. std::cout << "is Not a Var\n";
  690. }
  691. }
  692. } else if (state == 1) {
  693. // in func call, double check the (
  694. if (it == "(") {
  695. state = 2;
  696. } else {
  697. std::cout << "doExpression1 - func call did not have (\n";
  698. std::cout << "doExpression1 - stack is [" << stack << "] will now have to set it to [" << callFunc << "]\n";
  699. stack = callFunc;
  700. state = 0;
  701. }
  702. } else if (state == 2) {
  703. params = it;
  704. state = 3;
  705. } else if (state == 3) {
  706. // in func call, double check the )
  707. if (it == ")") {
  708. /*
  709. js_function *jfunc = dynamic_cast<js_function*>(stack);
  710. if (!jfunc) {
  711. std::cout << "Could cast [" << stack << "] to func\n";
  712. continue;
  713. }
  714. */
  715. if (!callFunc) {
  716. std::cout << "HALT callFunc is null!\n";
  717. continue;
  718. }
  719. std::cout << "doExpression3 - calling [" << callFuncName << "](" << params << ") at [" << callFunc << "] with [" << callFunc->tokens.size() << "]tokens\n";
  720. stack = doFunctionCall(callFunc, params, rootScope);
  721. state = 0;
  722. } else {
  723. std::cout << "doExpression3 - func call did not have (\n";
  724. std::cout << "doExpression3 - stack is [" << stack << "] will now have to set it to [" << callFunc << "]\n";
  725. stack = callFunc;
  726. state = 0;
  727. }
  728. } else if (state == 4) {
  729. js_internal_storage *expRes = doExpression(rootScope, it);
  730. // invert expRes;
  731. js_bool *jb = new js_bool;
  732. jb->value = !jsIsFalse(expRes);
  733. stack = jb;
  734. state = 0;
  735. } else if (state == 5) {
  736. js_function *objectScope = new js_function;
  737. js_object *newObject = new js_object;
  738. //doAssignment(*objectScope, it);
  739. parseJSON(*objectScope, it);
  740. // translate the scope into js_object
  741. //std::cout << "JSON object output" << std::endl;
  742. for(auto it2 : objectScope->locals.value) {
  743. //std::cout << "[" << it2.first << "=" << it2.second << "]" << std::endl;
  744. newObject->value[it2.first] = it2.second;
  745. }
  746. //std::cout << "JSON object done" << std::endl;
  747. stack = newObject;
  748. state = 0;
  749. } else {
  750. std::cout << "doExpression unknown state[" << std::to_string(state) << "]\n";
  751. }
  752. }
  753. // FIXME: any remainder?
  754. if (state == 1) {
  755. // x = func ref that's never called
  756. std::cout << "doExpressionEND1 - func call did not have (\n";
  757. std::cout << "doExpressionEND1 - stack is [" << stack << "] will now have to set it to [" << callFunc << "]\n";
  758. stack = callFunc;
  759. state = 0;
  760. }
  761. if (state != 0) {
  762. std::cout << "doExpression final state[" << std::to_string(state) << "]\n";
  763. }
  764. return stack;
  765. }
  766. size_t findClosing(std::string token, size_t start, char open, char close) {
  767. std::cout << "\nfindClosing start[" << token.substr(start) << "]\n";
  768. size_t parenLevel = 0;
  769. for (size_t cursor = start; cursor < token.length(); cursor++) {
  770. std::cout << "findClosing scanning[" << token[cursor] << "] at[" << cursor << "/" << token.length() << "] parenLevel[" << parenLevel << "]\n";
  771. if (token[cursor] == open) {
  772. parenLevel++;
  773. } else
  774. if (token[cursor] == close) {
  775. parenLevel--;
  776. if (!parenLevel) {
  777. return cursor;
  778. }
  779. }
  780. }
  781. std::cout << "findClosing - HALT didnt find closing element[" << close << "]\n";
  782. return token.length();
  783. }
  784. // start is right after the "function" 8
  785. size_t locateFunctionNameEnd(std::string source, size_t start) {
  786. std::cout << "\nlocateFunctionNameEnd start[" << source.substr(start) << "]\n";
  787. for (size_t cursor = start; cursor < source.length(); cursor++) {
  788. if (source[cursor] == '(') {
  789. return cursor;
  790. }
  791. }
  792. std::cout << "locateFunctionNameEnd - HALT didnt find start paren for function\n";
  793. return source.length();
  794. }
  795. // start is right after the "function name(" 8
  796. size_t locateFunctionParamsEnd(std::string source, size_t start) {
  797. std::cout << "\nlocateFunctionParamsEnd start[" << source.substr(start) << "]\n";
  798. for (size_t cursor = start; cursor < source.length(); cursor++) {
  799. if (source[cursor] == '{') {
  800. return cursor;
  801. }
  802. }
  803. std::cout << "locateFunctionParamsEnd - HALT didnt find start brace for function\n";
  804. return source.length();
  805. }
  806. size_t locateSingleQuoteEnd(const std::string source, const size_t start) {
  807. for (size_t cursor = start; cursor < source.length(); cursor++) {
  808. if (source[cursor] == '\'') {
  809. if (source[cursor - 1] != '\\') {
  810. return cursor;
  811. }
  812. }
  813. }
  814. return start;
  815. }
  816. size_t locateDoubleQuoteEnd(const std::string source, const size_t start) {
  817. for (size_t cursor = start; cursor < source.length(); cursor++) {
  818. if (source[cursor] == '"') {
  819. if (source[cursor - 1] != '\\') {
  820. return cursor;
  821. }
  822. }
  823. }
  824. return start;
  825. }
  826. // well a regex is like a variable, can only be right side
  827. // where a divide is between two variables
  828. // can't start on the /
  829. // also could be division
  830. // end on the / or the last modifier (/x/g)
  831. size_t locateRegexEnd(const std::string source, const size_t start) {
  832. size_t adj = 0;
  833. if (source[start]=='/') {
  834. std::cout << "WARNING starting locateRegexEnd on /\n";
  835. adj = 1;
  836. }
  837. std::cout << "locateRegexEnd start [" << source.substr(start) << "]\n";
  838. unsigned char state = 0;
  839. for (size_t cursor = start + adj; cursor < source.length(); cursor++) {
  840. std::cout << "locateRegexEnd step [" << cursor << "/" << source.length() << "] char[" << source[cursor] << "] state[" << std::to_string(state) << "]\n";
  841. switch(state) {
  842. case 0:
  843. if (source[cursor] == '/') {
  844. if (source[cursor - 1] != '\\') {
  845. state = 1;
  846. }
  847. }
  848. // we need to search until the end of the string for any potential closing element
  849. break;
  850. case 1:
  851. // ,;\n
  852. switch(source[cursor]) {
  853. case '.': // regex obj
  854. return cursor - 1;
  855. case ',':
  856. return cursor - 1;
  857. break;
  858. case ';':
  859. return cursor - 1;
  860. break;
  861. case '\n':
  862. return cursor - 1;
  863. break;
  864. case ')':
  865. return cursor - 1;
  866. break;
  867. case '}':
  868. return cursor - 1;
  869. break;
  870. }
  871. break;
  872. }
  873. }
  874. // could be division
  875. std::cout << "locateRegexEnd division?\n";
  876. // division confirmation
  877. state = 0;
  878. for (size_t cursor = start + adj; cursor < source.length(); cursor++) {
  879. std::cout << "locateRegexEnd divisionStep [" << cursor << "/" << source.length() << "] char[" << source[cursor] << "] state[" << std::to_string(state) << "]\n";
  880. switch(state) {
  881. case 0:
  882. if (source[cursor] == '/') {
  883. if (source[cursor - 1] != '\\') {
  884. state = 1;
  885. }
  886. }
  887. switch(source[cursor]) {
  888. case ',':
  889. return start;
  890. break;
  891. case ';':
  892. return start;
  893. break;
  894. case '\n':
  895. return start;
  896. break;
  897. // .)} ?
  898. }
  899. break;
  900. case 1:
  901. // ,;\n
  902. switch(source[cursor]) {
  903. case '.': // regex obj
  904. return cursor;
  905. break;
  906. case ',':
  907. return cursor;
  908. break;
  909. case ';':
  910. return cursor;
  911. break;
  912. case '\n':
  913. return cursor;
  914. break;
  915. case ')':
  916. return cursor - 1;
  917. break;
  918. case '}':
  919. return cursor - 1;
  920. break;
  921. }
  922. break;
  923. }
  924. }
  925. // what the hell is this...
  926. return start;
  927. }
  928. int getNextExpressionLevel = 0;
  929. // we return the terminator if ,;\n or )}
  930. // if the expression is a function definition, then we return the () as part of it
  931. // when returning a function block, it needs to end on the }
  932. size_t getNextExpression(const std::string source, const size_t start) {
  933. getNextExpressionLevel++;
  934. std::cout << "\n" << std::string(getNextExpressionLevel * 2, ' ') << "getNextExpression(" << getNextExpressionLevel << ") start[" << source.substr(start) << "]\n";
  935. unsigned char state = 0;
  936. size_t parenLevel = 0;
  937. size_t stateStart = 0;
  938. for (size_t cursor = start; cursor < source.length(); cursor++) {
  939. std::cout << std::string(getNextExpressionLevel * 2, ' ') << "getNextExpression(" << getNextExpressionLevel << ") scanning[" << source[cursor] << "] at[" << cursor << "/" << source.length() << "] state[" << std::to_string(state) << "] parenLevel[" << parenLevel << "]\n";
  940. switch(state) {
  941. case 0:
  942. // start function call
  943. if (source[cursor]=='(') {
  944. parenLevel++;
  945. stateStart = cursor;
  946. state = 1;
  947. } else
  948. if (source[cursor]=='\'') {
  949. state = 4;
  950. } else
  951. if (source[cursor]=='"') {
  952. state = 5;
  953. }
  954. // detect function definition and recurse
  955. if (source[cursor] == 'f' && source.length() > cursor + 7 && source[cursor + 1] == 'u'
  956. && source[cursor + 2] == 'n' && source[cursor + 3] == 'c' && source[cursor + 4] == 't'
  957. && source[cursor + 5] == 'i' && source[cursor + 6] == 'o' && source[cursor + 7] == 'n') {
  958. size_t last = cursor;
  959. // parse name
  960. last = locateFunctionNameEnd(source, cursor);
  961. // parse params
  962. last = locateFunctionParamsEnd(source, last);
  963. // you need to find first brace
  964. // call parseFunctionBody
  965. last = parseFunctionBody(source, last);
  966. std::cout << std::string(getNextExpressionLevel * 2, ' ') << "getNextExpression(" << getNextExpressionLevel << ") parseFunctionBody last[" << source[last] << "] just extracted[" << source.substr(cursor, last - cursor + 1) << "]\n";
  967. cursor = last + 1; // artificially inflact the end because we're going to strip it
  968. }
  969. // detect for
  970. // and end expression after }
  971. // start regex
  972. if (source[cursor]=='/' && source.length() > cursor + 1 && source[cursor + 1] != '/') {
  973. // is this a regex or division?
  974. size_t next = locateRegexEnd(source, cursor + 1);
  975. if (next != start) {
  976. // was a regex
  977. cursor = next;
  978. } // else was division element
  979. }
  980. // start scope
  981. if (source[cursor]=='{') {
  982. parenLevel++;
  983. state = 2;
  984. }
  985. // minification technique (or list of variables, still an end of an expression tho)
  986. if (source[cursor]==',') {
  987. getNextExpressionLevel--;
  988. return cursor;
  989. }
  990. // normal expression ender
  991. if (source[cursor]==';') {
  992. getNextExpressionLevel--;
  993. return cursor;
  994. }
  995. // modern experession ender
  996. if (source[cursor]=='\n') {
  997. getNextExpressionLevel--;
  998. return cursor;
  999. }
  1000. // this is the last expression in a function
  1001. if (source[cursor]=='}') {
  1002. getNextExpressionLevel--;
  1003. // -1 doesn't work if the previous call in this block was from parseFunctionBody
  1004. // but we need -1 when found a stray }
  1005. return cursor - 1; // need to include the end }
  1006. }
  1007. // this is the last element in a list
  1008. if (source[cursor]==')') {
  1009. getNextExpressionLevel--;
  1010. return cursor;
  1011. }
  1012. /// maybe && and || should be expression terminators too
  1013. break;
  1014. case 1: // inside a function call, (
  1015. // make sure function isn't in a quote
  1016. if (source[cursor]=='\'') {
  1017. cursor = locateSingleQuoteEnd(source, cursor + 1);
  1018. } else
  1019. if (source[cursor]=='"') {
  1020. cursor = locateDoubleQuoteEnd(source, cursor + 1);
  1021. }
  1022. // we can't do this because
  1023. // getNextExpression(1) start[!!e.tailSize&&(t=$.cls("replyContainer"),!(a=t[t.length-e.tailSize])||(a=$.cls("dateTime",a)[0],n=(0|e.lastUpdated/1e3)-+a.getAttribute("data-utc"),i=0|(Date.now()-e.lastUpdated)/1e3,n>i))}]
  1024. // after e.lastUpdated
  1025. // getNextExpression(1) scanning[/] at[192/267] state[1] parenLevel[3]
  1026. // locateRegexEnd start [1e3)-+a.getAttribute("data-utc"),i=0|(Date.now()-e.lastUpdated)/1e3,n>i))}]
  1027. // we're just not smart enough to tell if we're division or regex (left/right or middle)
  1028. // if we're right after the starting (, then we know we're a regex in all cases.
  1029. if (stateStart + 1 == cursor) {
  1030. // start regex
  1031. if (source[cursor]=='/') {
  1032. // is this a regex or division?
  1033. size_t next = locateRegexEnd(source, cursor + 1);
  1034. if (next != start) {
  1035. // was a regex
  1036. cursor = next;
  1037. } // else was division element
  1038. }
  1039. }
  1040. // (function() {})
  1041. // detect function definition and recurse
  1042. if (source[cursor] == 'f' && source.length() > cursor + 7 && source[cursor + 1] == 'u'
  1043. && source[cursor + 2] == 'n' && source[cursor + 3] == 'c' && source[cursor + 4] == 't'
  1044. && source[cursor + 5] == 'i' && source[cursor + 6] == 'o' && source[cursor + 7] == 'n') {
  1045. // parse name
  1046. cursor = locateFunctionNameEnd(source, cursor);
  1047. // parse params
  1048. cursor = locateFunctionParamsEnd(source, cursor);
  1049. // you need to find first brace
  1050. // call parseFunctionBody
  1051. cursor = parseFunctionBody(source, cursor);
  1052. std::cout << std::string(getNextExpressionLevel * 2, ' ') << "getNextExpression - after parseFunctionBody now at " << cursor << "/" << source.length() << " char[" << source[cursor] << "] prev[" << source[cursor - 1] << "]\n";
  1053. }
  1054. // find end of function call
  1055. if (source[cursor] == '(') {
  1056. parenLevel++;
  1057. } else
  1058. if (source[cursor] == ')') {
  1059. parenLevel--;
  1060. if (!parenLevel) {
  1061. // while nothing can happen after a function call
  1062. // we need to process the terminating ,; or \n
  1063. state = 0;
  1064. }
  1065. }
  1066. break;
  1067. case 2:
  1068. // find end of scope
  1069. if (source[cursor] == '{') {
  1070. parenLevel++;
  1071. } else
  1072. if (source[cursor] == '}') {
  1073. parenLevel--;
  1074. if (!parenLevel) {
  1075. state = 0;
  1076. }
  1077. }
  1078. break;
  1079. case 4:
  1080. if (source[cursor] == '\'') {
  1081. if (source[cursor - 1] != '\\') {
  1082. //quoteEnd = cursor - 1;
  1083. state = 0;
  1084. }
  1085. }
  1086. break;
  1087. case 5:
  1088. if (source[cursor] == '"') {
  1089. if (source[cursor - 1] != '\\') {
  1090. //quoteEnd = cursor - 1;
  1091. state = 0;
  1092. }
  1093. }
  1094. break;
  1095. }
  1096. }
  1097. std::cout << std::string(getNextExpressionLevel * 2, ' ') << "getNextExpression[" << source.substr(start) << "] - WARNING didnt find another expression\n";
  1098. return source.length();
  1099. }
  1100. // also finishing character would be good to return
  1101. // we just need to find the start and ends, we don't need to parse contents
  1102. // we'll parse contents on execution
  1103. // sometimes the token starts at function()
  1104. // othertimes inside function
  1105. // we're supposed to be parsing a function definition
  1106. // ok we should only parse function bodies after the parameters
  1107. // so no () unless after
  1108. // so do we start at { or right before? well we don't need to...
  1109. // start should be after {?
  1110. // we should return a character that points to }
  1111. size_t parseFunctionBodyLevel = 0;
  1112. size_t parseFunctionBody(std::string source, size_t start) {
  1113. parseFunctionBodyLevel++;
  1114. std::cout << "\nparseFunctionBody(" << parseFunctionBodyLevel << ") start[" << source.substr(start) << "] next8[" << source.substr(start, 8) << "]\n";
  1115. size_t state = 0;
  1116. size_t parenLevel = 0;
  1117. //size_t last = start ? start - 1 : 0;
  1118. for (size_t cursor = start; cursor < source.length(); cursor++) {
  1119. std::cout << "parseFunctionBody(" << parseFunctionBodyLevel << ") scanning[" << source[cursor] << "] at[" << cursor << "/" << source.length() << "] state[" << std::to_string(state) << "]\n";
  1120. switch(state) {
  1121. case 0: // look for function body start
  1122. if (source[cursor] == '{') {
  1123. std::cout << "detected{ at" << " cursor " << cursor << std::endl;
  1124. // there really shouldn't be anything between ) and {
  1125. parenLevel++;
  1126. //last = cursor + 1;
  1127. state = 1;
  1128. //cursor = findClosing(source, cursor, '{', '}');
  1129. // FIXME: take function string and send it to a tokenizer
  1130. // save tokens to func.tokens
  1131. //expression_parts.push_back("{");
  1132. //return cursor;
  1133. }
  1134. break;
  1135. case 1:
  1136. // we're not done with outer function, but we are with the inner
  1137. /*
  1138. if (source[cursor] == '}') {
  1139. parenLevel--;
  1140. if (!parenLevel) {
  1141. std::cout << "parseFunctionBody(" << parseFunctionBodyLevel << ") PRE final} [" << source.substr(start, cursor + 1 - start) << "]\n";
  1142. parseFunctionBodyLevel--;
  1143. return cursor; // return a character ending on } according to spec
  1144. }
  1145. }
  1146. */
  1147. // what we can enter that has {
  1148. // well a function
  1149. // so we need to step expression by expression tbh
  1150. size_t next = getNextExpression(source, cursor);
  1151. std::cout << "parseFunctionBody(" << parseFunctionBodyLevel << ") - state1 start[" << cursor << "] next[" << next << "]\n";
  1152. if (next < cursor) next = cursor; // advanced at least 1 char
  1153. std::string exp = source.substr(cursor, next + 1 - cursor);
  1154. exp = trim(exp);
  1155. std::cout << "parseFunctionBody(" << parseFunctionBodyLevel << ") - NextExpression [" << exp << "]\n";
  1156. // we're basically looking for a next expression of {
  1157. /*
  1158. if (exp.length() == 0) {
  1159. return cursor;
  1160. }
  1161. */
  1162. if (exp.length() == 1) {
  1163. if (exp == "{") {
  1164. parenLevel++;
  1165. } else
  1166. if (exp == "}") {
  1167. parenLevel--;
  1168. if (!parenLevel) {
  1169. // this doesn't mean the function is over
  1170. // how it can it not, we have equal amounts of level
  1171. // well the levels aren't at the character level
  1172. std::cout << "parseFunctionBody(" << parseFunctionBodyLevel << ") final} [" << source.substr(start, next + 1 - start) << "]\n";
  1173. parseFunctionBodyLevel--;
  1174. return next; // return a character ending on } according to spec
  1175. }
  1176. }
  1177. }
  1178. // FIXME: kind of an ugly hack
  1179. // can't just be () because the expression could just be a call itself
  1180. // needs to be }()
  1181. if (exp.length() > 2) {
  1182. std::string last3 = exp.substr(exp.length() - 3, 3);
  1183. std::cout << "last3[" << last3 << "]\n";
  1184. if (last3 == "}()") {
  1185. // function declartion & call
  1186. std::cout << "parseFunctionBody(" << parseFunctionBodyLevel << ") final}() [" << source.substr(start, cursor + 1 - start) << "]\n";
  1187. parseFunctionBodyLevel--;
  1188. return cursor;
  1189. }
  1190. }
  1191. cursor = next;
  1192. std::cout << "parseFunctionBody1(" << parseFunctionBodyLevel << ") now at " << cursor << "/" << source.length() << " char[" << source[cursor] << "]\n";
  1193. if (cursor == source.length() - 1) {
  1194. // we just ended on
  1195. if (source[cursor]=='}') {
  1196. parseFunctionBodyLevel--;
  1197. return cursor;
  1198. }
  1199. }
  1200. /*
  1201. if (source[cursor] == '{') {
  1202. parenLevel++;
  1203. } else
  1204. if (source[cursor] == '}') {
  1205. parenLevel--;
  1206. if (!parenLevel) {
  1207. return cursor;
  1208. }
  1209. }
  1210. */
  1211. break;
  1212. }
  1213. }
  1214. parseFunctionBodyLevel--;
  1215. std::cout << "parseFunctionBody(" << parseFunctionBodyLevel << ") - HALT didnt find closing element for function\n";
  1216. return source.length();
  1217. /*
  1218. //jsGetTokens will tokenize the entire body of the function
  1219. // and return it as one token
  1220. std::vector<std::string> tokens = jsGetTokens(source, start + 1); // +1 to skip {
  1221. std::cout << "\nparseFunctionBody - jsGetTokens got " << tokens.size() << " tokens\n";
  1222. for(auto it: tokens) {
  1223. std::cout << "[" << it << "]" "\n";
  1224. }
  1225. std::string funcBody = tokens[0];
  1226. return start + funcBody.length();
  1227. */
  1228. }
  1229. // don't include the start { or ending }
  1230. // prototype doesn't include ()
  1231. js_function *makeFunctionFromString(const std::string body, const std::string prototype, js_function *parent) {
  1232. std::cout << "makeFunctionFromString [" << prototype << "][" << body << "]\n";
  1233. js_function *func = new js_function;
  1234. func->tokens = jsGetTokens(body, 0);
  1235. func->parameters = split(prototype, ',');
  1236. if (!func->tokens.size()) {
  1237. std::cout << "empty function?\n";
  1238. }
  1239. func->parentScope = parent;
  1240. return func;
  1241. }
  1242. void parseArray(js_function &rootScope, std::string token) {
  1243. std::vector<std::string> json_keys;
  1244. std::vector<std::string> json_values;
  1245. size_t cursor;
  1246. size_t last = 0;
  1247. size_t quoteStart = 0;
  1248. size_t quoteEnd = 0;
  1249. size_t parenStart = 0;
  1250. size_t parenLevel = 0;
  1251. unsigned char state = 0;
  1252. for (cursor = 0; cursor < token.length(); cursor++) {
  1253. // we should only look for [:'"]
  1254. if (state == 0) {
  1255. switch(token[cursor]) {
  1256. // { or [ could be expression parsed I think...
  1257. case '\'':
  1258. quoteStart = cursor;
  1259. state = 4;
  1260. break;
  1261. case '"':
  1262. quoteStart = cursor;
  1263. state = 5;
  1264. break;
  1265. case ',':
  1266. std::string key = "";
  1267. if (quoteStart) {
  1268. // constant
  1269. key = token.substr(quoteStart + 1, quoteEnd - quoteStart); // skip the quotes
  1270. } else {
  1271. // variable
  1272. key = token.substr(last, cursor - last);
  1273. }
  1274. key = trim(key);
  1275. std::cout << "element[" << key << "] quoted: " << quoteStart << std::endl;
  1276. json_keys.push_back(key);
  1277. last = cursor + 1;
  1278. state = 2;
  1279. quoteStart = 0;
  1280. quoteEnd = 0;
  1281. break;
  1282. }
  1283. } else if (state == 2) { // get value state
  1284. // now we haven't to make sure we don't enter {, } or function
  1285. // ', " are ok, but have to turn off parsing until we encounter the next
  1286. // a-zA-Z$ for variables assignment
  1287. switch(state) {
  1288. case 0:
  1289. switch(token[cursor]) {
  1290. case '{':
  1291. parenStart = cursor;
  1292. parenLevel++;
  1293. state = 1;
  1294. break;
  1295. case '[':
  1296. parenStart = cursor;
  1297. parenLevel++;
  1298. state = 2;
  1299. break;
  1300. case '\'':
  1301. quoteStart = cursor;
  1302. state = 4;
  1303. break;
  1304. case '"':
  1305. quoteStart = cursor;
  1306. state = 5;
  1307. break;
  1308. case ',':
  1309. std::string value = "";
  1310. if (quoteStart) {
  1311. value = token.substr(quoteStart + 1, quoteEnd - quoteStart); // skip the quotes
  1312. } else {
  1313. value = token.substr(last, cursor - last);
  1314. value = trim(value);
  1315. }
  1316. std::cout << "value[" << value << "] quoted" << quoteStart << std::endl;
  1317. // JSON objects will already be assigned and empty
  1318. if (value != "") {
  1319. json_values.push_back(value);
  1320. js_string *newString = new js_string();
  1321. newString->value = value;
  1322. rootScope.locals.value[json_keys.back()] = newString;
  1323. }
  1324. last = cursor + 1;
  1325. state = 0;
  1326. quoteStart = 0;
  1327. quoteEnd = 0;
  1328. break;
  1329. }
  1330. break;
  1331. case 1:
  1332. switch(token[cursor]) {
  1333. case '{':
  1334. parenLevel++;
  1335. break;
  1336. case '}':
  1337. parenLevel--;
  1338. if (!parenLevel) {
  1339. std::string JSON = token.substr(parenStart + 1, cursor - parenStart);
  1340. std::cout << "PA Need to deJSON [" << JSON << "]" << std::endl;
  1341. // well we can get a scope back
  1342. js_function *objectScope = new js_function;
  1343. parseJSON(*objectScope, JSON);
  1344. js_object *newObject = new js_object;
  1345. rootScope.locals.value[json_keys.back()] = newObject;
  1346. last = cursor + 1;
  1347. state = 0;
  1348. }
  1349. break;
  1350. }
  1351. break;
  1352. case 2:
  1353. switch(token[cursor]) {
  1354. case '[':
  1355. parenLevel++;
  1356. break;
  1357. case ']':
  1358. parenLevel--;
  1359. if (!parenLevel) {
  1360. std::string JSON = token.substr(parenStart + 1, cursor - parenStart);
  1361. std::cout << "Need to deArray [" << JSON << "]" << std::endl;
  1362. // well we can get a scope back
  1363. js_function *objectScope = new js_function;
  1364. parseJSON(*objectScope, JSON);
  1365. js_object *newObject = new js_object;
  1366. rootScope.locals.value[json_keys.back()] = newObject;
  1367. last = cursor + 1;
  1368. state = 0;
  1369. }
  1370. break;
  1371. }
  1372. break;
  1373. case 4:
  1374. if (token[cursor] == '\'') {
  1375. if (token[cursor - 1] != '\\') {
  1376. quoteEnd = cursor - 1;
  1377. state = 0;
  1378. }
  1379. }
  1380. break;
  1381. case 5:
  1382. if (token[cursor] == '"') {
  1383. if (token[cursor - 1] != '\\') {
  1384. quoteEnd = cursor - 1;
  1385. state = 0;
  1386. }
  1387. }
  1388. break;
  1389. }
  1390. } else if (state == 4) {
  1391. if (token[cursor] == '\'') {
  1392. if (token[cursor - 1] != '\\') {
  1393. quoteEnd = cursor - 1;
  1394. state = 0;
  1395. }
  1396. }
  1397. } else if (state == 5) {
  1398. if (token[cursor] == '"') {
  1399. if (token[cursor - 1] != '\\') {
  1400. quoteEnd = cursor - 1;
  1401. state = 0;
  1402. }
  1403. }
  1404. }
  1405. }
  1406. }
  1407. //
  1408. void parseJSON(js_function &rootScope, std::string token) {
  1409. std::vector<std::string> json_keys;
  1410. std::vector<std::string> json_values;
  1411. size_t cursor;
  1412. size_t last = 0;
  1413. size_t quoteStart = 0;
  1414. size_t quoteEnd = 0;
  1415. size_t parenStart = 0;
  1416. size_t parenLevel = 0;
  1417. unsigned char keyState = 0;
  1418. unsigned char valueState = 0;
  1419. js_function *func = nullptr;
  1420. std::cout << "parseJSON start[" << token << "]\n";
  1421. for (cursor = 0; cursor < token.length(); cursor++) {
  1422. std::cout << "parseJSON step cursor[" << cursor << "/" << token.length() << "] char[" << token[cursor] << "] keyState[" << std::to_string(keyState) << "] valueState[" << std::to_string(valueState) << "]\n";
  1423. if (keyState == 0) {
  1424. valueState = 0; // reset value state
  1425. switch(token[cursor]) {
  1426. case '\'':
  1427. quoteStart = cursor;
  1428. keyState = 4;
  1429. break;
  1430. case '"':
  1431. quoteStart = cursor;
  1432. keyState = 5;
  1433. break;
  1434. case ':':
  1435. std::string key = "";
  1436. if (quoteStart) {
  1437. key = token.substr(quoteStart + 1, quoteEnd - quoteStart); // skip the quotes
  1438. } else {
  1439. key = token.substr(last, cursor - last);
  1440. }
  1441. key = trim(key);
  1442. std::cout << "parsingJSON - key[" << key << "] quoted: " << quoteStart << std::endl;
  1443. if (key == "prettyPrint") {
  1444. std::cout << "Found your shit, now step it\n";
  1445. }
  1446. json_keys.push_back(key);
  1447. last = cursor + 1;
  1448. keyState = 2;
  1449. quoteStart = 0;
  1450. quoteEnd = 0;
  1451. break;
  1452. }
  1453. } else if (keyState == 2) { // get value state
  1454. // now we haven't to make sure we don't enter {, } or function
  1455. // ', " are ok, but have to turn off parsing until we encounter the next
  1456. // a-zA-Z$ for variables assignment
  1457. // we should only look for [:'"]
  1458. //std::cout << "parseJSON ks2 looking at [" << token[cursor] << "] vs[" << std::to_string(valueState) << "]\n";
  1459. switch(valueState) {
  1460. case 0:
  1461. switch(token[cursor]) {
  1462. case 'f':
  1463. if (cursor + 7 < token.length()) {
  1464. std::string next8 = token.substr(cursor, 8);
  1465. //std::cout << "parsingJSON - isFunction [" << next8 << "]" << std::endl;
  1466. if (next8 == "function") {
  1467. cursor = locateFunctionNameEnd(token, cursor);
  1468. last = locateFunctionParamsEnd(token, cursor);
  1469. std::string prototype = token.substr(cursor + 1, last - cursor - 1);
  1470. cursor = last;
  1471. // you need to find first brace
  1472. last = parseFunctionBody(token, cursor);
  1473. if (token[last]!='}') {
  1474. // should end on }
  1475. // but should it be last + 1? definitely not
  1476. std::cout << "parsingJSON - parseFunctionBody broke spec, ending on [" << token[last] << "]\n";
  1477. }
  1478. //std::cout << "parseJSON last[" << token[last] << "]\n";
  1479. // + 1 to skip initial { and not + 1 because } and -1 because of that starting + 1
  1480. std::string funcContents = token.substr(cursor + 1, last - cursor - 1);
  1481. std::cout << "parsingJSON - function " << json_keys.back() << "[" << prototype << "] [" << funcContents << "]" << std::endl;
  1482. std::cout << "parsingJSON - current char [" << token[last] << "]\n";
  1483. cursor = last; // continue after } (for loop will advance this)
  1484. // we have this key now but we'll wait for the , to do it's thing
  1485. //valueState = 6;
  1486. json_values.push_back(funcContents);
  1487. // [" << funcContents << "]
  1488. assignfile << "JSON." << json_keys.back() << "=" << "_NTRFUNC0[" << funcContents.length() << "]" << "\n";
  1489. rootScope.locals.value[json_keys.back()] = makeFunctionFromString(funcContents, prototype, &rootScope);
  1490. valueState = 6;
  1491. //keyState = 0;
  1492. //valueState = 0;
  1493. }
  1494. }
  1495. break;
  1496. case '{':
  1497. parenStart = cursor;
  1498. parenLevel++;
  1499. valueState = 1;
  1500. break;
  1501. case '[':
  1502. parenStart = cursor;
  1503. parenLevel++;
  1504. valueState = 2;
  1505. break;
  1506. case '\'':
  1507. quoteStart = cursor;
  1508. valueState = 4;
  1509. break;
  1510. case '"':
  1511. quoteStart = cursor;
  1512. valueState = 5;
  1513. break;
  1514. case ',':
  1515. std::string value = "";
  1516. if (quoteStart) {
  1517. value = token.substr(quoteStart + 1, quoteEnd - quoteStart); // skip the quotes
  1518. } else {
  1519. value = token.substr(last, cursor - last);
  1520. value = trim(value);
  1521. }
  1522. //std::cout << "parsingJSON - value[" << value << "] quoted" << quoteStart << std::endl;
  1523. // JSON objects will already be assigned and empty
  1524. if (value != "") {
  1525. json_values.push_back(value);
  1526. js_string *newString = new js_string();
  1527. newString->value = value;
  1528. assignfile << "JSON." << json_keys.back() << "=_NTRSTRING(,):'" << value << "'\n";
  1529. rootScope.locals.value[json_keys.back()] = newString;
  1530. } else {
  1531. if (quoteStart) {
  1532. // likely ""
  1533. json_values.push_back("");
  1534. js_string *newString = new js_string();
  1535. newString->value = "";
  1536. assignfile << "JSON." << json_keys.back() << "=_NTRSTRING(,):'" << value << "'\n";
  1537. rootScope.locals.value[json_keys.back()] = newString;
  1538. }
  1539. }
  1540. last = cursor + 1;
  1541. std::cout << "remainder last now points to[" << token[last] << "] should not be comma\n";
  1542. keyState = 0;
  1543. valueState = 0;
  1544. quoteStart = 0;
  1545. quoteEnd = 0;
  1546. break;
  1547. }
  1548. break;
  1549. case 1:
  1550. switch(token[cursor]) {
  1551. case '{':
  1552. parenLevel++;
  1553. break;
  1554. case '}':
  1555. parenLevel--;
  1556. if (!parenLevel) {
  1557. std::string JSON = token.substr(parenStart + 1, cursor - parenStart);
  1558. //std::cout << "parsingJSON - PJ Need to deJSON [" << JSON << "]" << std::endl;
  1559. // well we can get a scope back
  1560. js_function *objectScope = new js_function;
  1561. parseJSON(*objectScope, JSON);
  1562. js_object *newObject = new js_object;
  1563. assignfile << "JSON." << json_keys.back() << "=" << "_NTRJSON" << "\n";
  1564. rootScope.locals.value[json_keys.back()] = newObject;
  1565. last = cursor + 1;
  1566. valueState = 0;
  1567. // let keyState 2 finish it out
  1568. }
  1569. break;
  1570. }
  1571. break;
  1572. case 2:
  1573. switch(token[cursor]) {
  1574. case '[':
  1575. parenLevel++;
  1576. break;
  1577. case ']':
  1578. parenLevel--;
  1579. if (!parenLevel) {
  1580. std::string arrayStr = token.substr(parenStart + 1, cursor - parenStart);
  1581. //std::cout << "parsingJSON - Need to deArray [" << arrayStr << "]" << std::endl;
  1582. // well we can get a scope back
  1583. js_function *arrayScope = new js_function;
  1584. parseArray(*arrayScope, arrayStr);
  1585. js_array *newArray = new js_array;
  1586. assignfile << "JSON." << json_keys.back() << "=" << "_NTRARRAY" << "\n";
  1587. rootScope.locals.value[json_keys.back()] = newArray;
  1588. last = cursor + 1;
  1589. valueState = 0;
  1590. keyState = 0;
  1591. }
  1592. break;
  1593. }
  1594. break;
  1595. case 4:
  1596. if (token[cursor] == '\'') {
  1597. if (token[cursor - 1] != '\\') {
  1598. quoteEnd = cursor - 1;
  1599. valueState = 0;
  1600. }
  1601. }
  1602. break;
  1603. case 5:
  1604. if (token[cursor] == '"') {
  1605. if (token[cursor - 1] != '\\') {
  1606. quoteEnd = cursor - 1;
  1607. valueState = 0;
  1608. }
  1609. }
  1610. break;
  1611. case 6: // function push back
  1612. std::cout << "parseJSON state6 char[" << token[cursor] << "]\n";
  1613. if (token[cursor] == ',') {
  1614. /*
  1615. std::string value = "";
  1616. if (quoteStart) {
  1617. value = token.substr(quoteStart + 1, quoteEnd - quoteStart); // skip the quotes
  1618. } else {
  1619. value = token.substr(last, cursor - last);
  1620. value = trim(value);
  1621. }
  1622. std::cout << "parsingJSON - value[" << value << "] quoted" << quoteStart << std::endl;
  1623. if (value != "") {
  1624. json_values.push_back(value);
  1625. assignfile << "JSON." << json_keys.back() << "=" << "_NTRFUNC6" << "\n";
  1626. rootScope.data[json_keys.back()] = func;
  1627. }
  1628. */
  1629. last = cursor + 1; // set to after the current character (next char after ,)
  1630. keyState = 0;
  1631. valueState = 0;
  1632. quoteStart = 0;
  1633. quoteEnd = 0;
  1634. } else if (token[cursor] == '}') {
  1635. // it's the end of the JSON
  1636. keyState = 0;
  1637. }
  1638. break;
  1639. }
  1640. } else if (keyState == 4) {
  1641. if (token[cursor] == '\'') {
  1642. if (token[cursor - 1] != '\\') {
  1643. quoteEnd = cursor - 1;
  1644. keyState = 0;
  1645. }
  1646. }
  1647. } else if (keyState == 5) {
  1648. if (token[cursor] == '"') {
  1649. if (token[cursor - 1] != '\\') {
  1650. quoteEnd = cursor - 1;
  1651. keyState = 0;
  1652. }
  1653. }
  1654. }
  1655. }
  1656. std::cout << "done parsingJSON keyState[" << std::to_string(keyState) << "] valueState[" << std::to_string(valueState) << "]\n";
  1657. // we can end safely if after a function, likely just the } left over
  1658. if (keyState == 2 && valueState != 6) {
  1659. std::string value = "";
  1660. if (quoteStart) {
  1661. value = token.substr(quoteStart + 1, quoteEnd - quoteStart); // skip the quotes
  1662. } else {
  1663. value = token.substr(last, cursor - last);
  1664. value = trim(value);
  1665. }
  1666. //std::cout << "parsingJSON - value[" << value << "] quoted" << quoteStart << std::endl;
  1667. // JSON objects will already be assigned and empty
  1668. if (value != "") {
  1669. json_values.push_back(value);
  1670. js_string *newString = new js_string();
  1671. newString->value = value;
  1672. assignfile << "JSON." << json_keys.back() << "=_NTRSTRING(END):'" << value << "'\n";
  1673. rootScope.locals.value[json_keys.back()] = newString;
  1674. }
  1675. }
  1676. std::cout << "exiting parseJSON with: \n";
  1677. jsDisplayScope(&rootScope, 0);
  1678. }
  1679. // return value is if there was any error (false for error, true for no error)
  1680. int doAssignmentLevel = 0;
  1681. bool doAssignment(js_function &rootScope, std::string token) {
  1682. doAssignmentLevel++;
  1683. // FIXME: make sure = isn't in quotes or JSON?
  1684. // FIXME: double or triple equal differentiation
  1685. //std::cout << "looking at [" << token << "]" << std::endl;
  1686. // document.documentElement.classList?($.hasClass=function(e,t){return e.classList.contains(t)},$.addClass=function(e,t){e.classList.add(t)},$.removeClass=function(e,t){e.classList.remove(t)}):($.hasClass=function(e,t){return-1!=(" "+e.className+" ").indexOf(" "+t+" ")},$.addClass=function(e,t){e.className=""===e.className?t:e.className+" "+t},$.removeClass=function(e,t){e.className=(" "+e.className+" ").replace(" "+t+" ","")})
  1687. std::vector<std::string> expression_parts;
  1688. size_t cursor;
  1689. size_t last = 0;
  1690. size_t quoteStart = 0;
  1691. size_t parenStart = 0;
  1692. size_t parenLevel = 0;
  1693. size_t trinaryLevel = 0;
  1694. unsigned char state = 0;
  1695. for (cursor = 0; cursor < token.length(); cursor++) {
  1696. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment(" << doAssignmentLevel << ") step [" << cursor << "/" << token.length() << "] char[" << token[cursor] << "] state[" << std::to_string(state) << "] parenLevel[" << parenLevel << "]\n";
  1697. if (state == 0) {
  1698. // =
  1699. // ||
  1700. // &&
  1701. // <, >, <=, >=, ==, ===, !=, !==
  1702. // +, -
  1703. // *, /, %
  1704. // ?, >>, <<
  1705. if (token[cursor] == 'f' && token.length() > cursor + 7 && token[cursor + 1] == 'u'
  1706. && token[cursor + 2] == 'n' && token[cursor + 3] == 'c' && token[cursor + 4] == 't'
  1707. && token[cursor + 5] == 'i' && token[cursor + 6] == 'o' && token[cursor + 7] == 'n') {
  1708. state = 3;
  1709. }
  1710. // if? yea we have to, otherwise it's scope becomes a JSON decode
  1711. // but that's not exactly a problem
  1712. // else?
  1713. if (token[cursor] == '\'') {
  1714. quoteStart = cursor;
  1715. state = 4;
  1716. } else
  1717. if (token[cursor] == '"') {
  1718. quoteStart = cursor;
  1719. state = 5;
  1720. } else
  1721. if (token[cursor] == '(') {
  1722. parenStart = cursor;
  1723. parenLevel++;
  1724. state = 8;
  1725. if (last != cursor) {
  1726. expression_parts.push_back(token.substr(last, cursor - last));
  1727. }
  1728. last = cursor + 1;
  1729. expression_parts.push_back("(");
  1730. } else
  1731. if (token[cursor] == '{') {
  1732. parenStart = cursor;
  1733. parenLevel++;
  1734. state = 1;
  1735. //std::cout << "last " << last << " cursor " << cursor << std::endl;
  1736. if (last != cursor) {
  1737. expression_parts.push_back(token.substr(last, cursor - last - 1));
  1738. }
  1739. last = cursor + 1;
  1740. expression_parts.push_back("{");
  1741. }
  1742. // single =
  1743. if (token[cursor] == '=' && token.length() > cursor + 1 && token[cursor + 1] != '=') {
  1744. //state = 1;
  1745. //std::cout << "starting = at " << cursor << " last: " << last << std::endl;
  1746. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1747. expression_parts.push_back("=");
  1748. }
  1749. // hack for JSON parsing
  1750. if (token[cursor] == ':') {
  1751. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1752. expression_parts.push_back("=");
  1753. state = 2;
  1754. }
  1755. // ||
  1756. if (token[cursor] == '|' && token.length() > cursor + 1 && token[cursor + 1] == '|') {
  1757. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 2;
  1758. expression_parts.push_back("||");
  1759. cursor++;
  1760. }
  1761. if (token[cursor] == '&' && token.length() > cursor + 1 && token[cursor + 1] == '&') {
  1762. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 2;
  1763. expression_parts.push_back("&&");
  1764. cursor++;
  1765. }
  1766. if (token[cursor] == '>' && token.length() > cursor + 1 && token[cursor + 1] != '=' && token[cursor + 1] != '>') {
  1767. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1768. expression_parts.push_back(">");
  1769. cursor++;
  1770. }
  1771. if (token[cursor] == '<' && token.length() > cursor + 1 && token[cursor + 1] != '=' && token[cursor + 1] != '<') {
  1772. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1773. expression_parts.push_back("<");
  1774. cursor++;
  1775. }
  1776. if (token[cursor] == '<' && token.length() > cursor + 1 && token[cursor + 1] == '=') {
  1777. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 2;
  1778. expression_parts.push_back("<=");
  1779. cursor++;
  1780. }
  1781. if (token[cursor] == '>' && token.length() > cursor + 1 && token[cursor + 1] == '=') {
  1782. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 2;
  1783. expression_parts.push_back(">=");
  1784. cursor++;
  1785. }
  1786. if (token[cursor] == '=' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] != '=') {
  1787. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 2;
  1788. expression_parts.push_back("==");
  1789. cursor++;
  1790. }
  1791. if (token[cursor] == '=' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] == '=') {
  1792. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 3;
  1793. expression_parts.push_back("===");
  1794. cursor+=2;
  1795. }
  1796. if (token[cursor] == '!' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] != '=') {
  1797. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 2;
  1798. expression_parts.push_back("!=");
  1799. cursor++;
  1800. }
  1801. if (token[cursor] == '!' && token.length() > cursor + 2 && token[cursor + 1] == '=' && token[cursor + 2] == '=') {
  1802. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 3;
  1803. expression_parts.push_back("!==");
  1804. cursor+=2;
  1805. }
  1806. // +
  1807. if (token[cursor] == '+') {
  1808. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1809. expression_parts.push_back("+");
  1810. } else
  1811. if (token[cursor] == '-') {
  1812. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1813. expression_parts.push_back("+");
  1814. } else
  1815. if (token[cursor] == '*') {
  1816. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1817. expression_parts.push_back("*");
  1818. } else
  1819. if (token[cursor] == '/') {
  1820. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1821. expression_parts.push_back("/");
  1822. } else
  1823. if (token[cursor] == '%') {
  1824. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1825. expression_parts.push_back("%");
  1826. } else
  1827. if (token[cursor] == '!') {
  1828. if (last != cursor) {
  1829. expression_parts.push_back(token.substr(last, cursor - last));
  1830. }
  1831. expression_parts.push_back("!"); last = cursor + 1;
  1832. } else
  1833. if (token[cursor] == '?') {
  1834. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1835. expression_parts.push_back("?");
  1836. trinaryLevel++;
  1837. state = 9;
  1838. } else if (token[cursor] == 'i' && token.length() > cursor + 2 && token[cursor + 1] == 'n' && token[cursor + 2] == ' ') {
  1839. // "property"in object (or new object)
  1840. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1841. expression_parts.push_back("in");
  1842. }
  1843. } else if (state == 1) {
  1844. if (token[cursor] == '{') {
  1845. parenLevel++;
  1846. } else
  1847. if (token[cursor] == '}') {
  1848. parenLevel--;
  1849. if (!parenLevel) {
  1850. expression_parts.push_back(token.substr(last, cursor - last));
  1851. last = cursor + 1;
  1852. expression_parts.push_back("}");
  1853. state = 0;
  1854. }
  1855. }
  1856. } else if (state == 2) { // json state (can be moved)
  1857. if (token[cursor] == ',') {
  1858. expression_parts.push_back(token.substr(last, cursor - last));
  1859. last = cursor + 1;
  1860. state = 0;
  1861. }
  1862. } else if (state == 3) { // function start (can be moved)
  1863. if (token[cursor] == '{') {
  1864. // lets put the function prototype
  1865. expression_parts.push_back(token.substr(last, cursor - last));
  1866. last = cursor + 1;
  1867. size_t next = parseFunctionBody(token, cursor);
  1868. std::string funcBody = token.substr(cursor, 1 + next - cursor);
  1869. cursor = next;
  1870. std::cout << std::string(doAssignmentLevel * 2, ' ') << "parseFunctionBody returned[" << funcBody << "]\n";
  1871. //std::cout << "doAssignment3 parseFunctionBody last[" << token[cursor] << "]\n";
  1872. expression_parts.push_back(funcBody);
  1873. last = cursor + 1;
  1874. expression_parts.push_back("}"); // end function
  1875. state = 7; // move to execution/call check
  1876. //std::cout << "doAssignment - s3 - parenLevel: " << parenLevel << "\n";
  1877. //parenLevel++;
  1878. //state = 6; // move to the function body
  1879. }
  1880. } else if (state == 4) {
  1881. if (token[cursor] == '\'') {
  1882. if (token[cursor - 1] != '\\') {
  1883. //std::string quote = token.substr(quoteStart + 1, cursor - quoteStart - 1);
  1884. //expression_parts.push_back(quote);
  1885. //std::cout << "single quote: " << quote << std::endl;
  1886. state = 0;
  1887. }
  1888. }
  1889. } else if (state == 5) {
  1890. if (token[cursor] == '"') {
  1891. if (token[cursor - 1] != '\\') {
  1892. //std::string quote = token.substr(quoteStart + 1, cursor - quoteStart - 1);
  1893. //expression_parts.push_back(quote);
  1894. //std::cout << "double quote: " << quote << std::endl;
  1895. state = 0;
  1896. }
  1897. }
  1898. } else if (state == 6) {
  1899. // function body
  1900. // now regexes can have unbalanced {}
  1901. cursor = parseFunctionBody(token, cursor);
  1902. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment6 parseFunctionBody first[" << token[cursor] << "] last[" << token[last] << "]\n";
  1903. expression_parts.push_back(token.substr(last, cursor - last));
  1904. last = cursor + 1;
  1905. expression_parts.push_back("}"); // end function
  1906. state = 7;
  1907. /*
  1908. if (token[cursor] == '{') {
  1909. parenLevel++;
  1910. } else
  1911. if (token[cursor] == '}') {
  1912. parenLevel--;
  1913. if (!parenLevel) {
  1914. expression_parts.push_back(token.substr(last, cursor - last));
  1915. last = cursor + 1;
  1916. expression_parts.push_back("}"); // end function
  1917. state = 7;
  1918. }
  1919. }*/
  1920. } else if (state == 7) {
  1921. // cursor should be passed }
  1922. switch(token[cursor]) {
  1923. case '(':
  1924. if (!parenLevel) {
  1925. expression_parts.push_back("("); // start function call
  1926. }
  1927. parenLevel++;
  1928. break;
  1929. case ')':
  1930. parenLevel--;
  1931. if (!parenLevel) {
  1932. expression_parts.push_back(token.substr(last, cursor - last));
  1933. last = cursor + 1;
  1934. expression_parts.push_back(")"); // end function call
  1935. state = 0;
  1936. }
  1937. break;
  1938. case '\n':
  1939. last = cursor + 1;
  1940. state = 0;
  1941. break;
  1942. case ';':
  1943. last = cursor + 1;
  1944. state = 0;
  1945. break;
  1946. case ',':
  1947. last = cursor + 1;
  1948. state = 0;
  1949. break;
  1950. }
  1951. } else if (state == 8) {
  1952. if (token[cursor] == '(') {
  1953. parenLevel++;
  1954. } else
  1955. if (token[cursor] == ')') {
  1956. parenLevel--;
  1957. if (!parenLevel) {
  1958. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1959. expression_parts.push_back(")");
  1960. state = 0;
  1961. }
  1962. }
  1963. } else if (state == 9) {
  1964. if (token[cursor] == '?') {
  1965. trinaryLevel++;
  1966. } else
  1967. if (token[cursor] == ':') {
  1968. trinaryLevel--;
  1969. if (!trinaryLevel) {
  1970. expression_parts.push_back(token.substr(last, cursor - last)); last = cursor + 1;
  1971. expression_parts.push_back(":");
  1972. state = 0;
  1973. }
  1974. }
  1975. }
  1976. }
  1977. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment ending state " << std::to_string(state) << std::endl;
  1978. if (last != token.length() && cursor != last) {
  1979. expression_parts.push_back(token.substr(last, token.length()));
  1980. if (state == 6) {
  1981. expression_parts.push_back("}");
  1982. }
  1983. }
  1984. std::cout << std::string(doAssignmentLevel * 2, ' ') << "expression string[" << token << "]" << std::endl;
  1985. std::cout << std::string(doAssignmentLevel * 2, ' ') << "expression debug" << std::endl;
  1986. std::string lastToken = "";
  1987. state = 0;
  1988. std::string left = "";
  1989. if (expression_parts.size() == 1) {
  1990. // usually just a variable declaration
  1991. std::string tLeft = trim(expression_parts[0]);
  1992. if (tLeft[0]=='"' && tLeft[tLeft.length() - 1]=='"') {
  1993. //std::cout << "dequoting[" << tLeft << "]" << std::endl;
  1994. tLeft=tLeft.substr(1, tLeft.length() - 2);
  1995. }
  1996. std::cout << std::string(doAssignmentLevel * 2, ' ') << "Assigning (end) [" << tLeft << "]" << std::endl;
  1997. // we're just initialling a blank variable
  1998. js_internal_storage **storage = getObjectKeyPointer(tLeft, &rootScope);
  1999. if (storage != nullptr) {
  2000. storage = nullptr; // FIXME;
  2001. } else {
  2002. rootScope.locals.value[tLeft] = nullptr;
  2003. }
  2004. std::cout << std::string(doAssignmentLevel * 2, ' ') << "expression end" << std::endl << std::endl;
  2005. doAssignmentLevel--;
  2006. return true;
  2007. }
  2008. bool negate = false;
  2009. js_function *func = nullptr;
  2010. js_function *callFunc = nullptr;
  2011. std::string prototype = "";
  2012. std::string params = "";
  2013. for(auto it : expression_parts) {
  2014. // probably should trim these
  2015. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment expression(" << doAssignmentLevel << ") token[" << it << "]" << std::to_string(state) << std::endl;
  2016. // default: put token in left, get op (usually =) and then get right side
  2017. if (state==0) {
  2018. negate = false;
  2019. prototype = "";
  2020. if (it == "=") {
  2021. left = lastToken;
  2022. state = 1;
  2023. }
  2024. if (it == "&&") {
  2025. // if stack is false...
  2026. //return true;
  2027. }
  2028. // can actually go to 9 (include the space)
  2029. std::string first8 = it.substr(0, 8);
  2030. if (first8 == "function") {
  2031. left = it.substr(8);
  2032. // well it can be "function()"
  2033. // no left and no starting brace...
  2034. cursor = locateFunctionParamsEnd(left, 0);
  2035. if (cursor != 0) {
  2036. left = left.substr(0, cursor);
  2037. }
  2038. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment - function expr start Adjust left[" << left << "] to [" << left.substr(0, cursor) << "]\n";
  2039. state = 3;
  2040. }
  2041. // most likely a function call
  2042. // but also can be an expression
  2043. // (function() {}())
  2044. // token will matter here
  2045. // check for ()
  2046. if (it == "(") {
  2047. state = 8;
  2048. }
  2049. } else if (state == 1) {
  2050. if (it == "") continue; // skip empties, don't fire the assignment too early
  2051. if (it == " ") continue; // skip empties, don't fire the assignment too early
  2052. if (it == "!") continue; // valid expression
  2053. if (it == "}") continue; // valid part of an expression (why are we getting this, should have a starting { and be in state 2)
  2054. // so these next 2 execute a function that's on the stack
  2055. // state 0: elem =
  2056. // state 1: funcName,(,proto,),;
  2057. if (it == "(") continue; // valid part of an expression
  2058. if (it == ")") continue; // valid part of an expression
  2059. if (it == "&&") continue; // valid part of an expression
  2060. if (it == "{") {
  2061. state = 2;
  2062. } else {
  2063. js_internal_storage *storage = doExpression(rootScope, it);
  2064. if (storage) {
  2065. js_function *funcTest = dynamic_cast<js_function*>(storage);
  2066. if (funcTest) {
  2067. callFunc = funcTest;
  2068. state = 9;
  2069. continue;
  2070. }
  2071. std::string tLeft = trim(left);
  2072. if (tLeft[0]=='"' && tLeft[tLeft.length() - 1]=='"') {
  2073. //std::cout << "dequoting[" << tLeft << "]" << std::endl;
  2074. tLeft=tLeft.substr(1, tLeft.length() - 2);
  2075. }
  2076. bool alreadySet = false;
  2077. size_t dotPos = tLeft.find('.');
  2078. if (dotPos != std::string::npos) {
  2079. js_internal_storage **key = getObjectKeyPointer(tLeft, &rootScope);
  2080. if (key != nullptr) {
  2081. alreadySet = true;
  2082. *key = storage;
  2083. }
  2084. }
  2085. if (!alreadySet) {
  2086. std::cout << "doAssignment final assign[" << tLeft << "] of type[" << typeOfStorage(storage) << "] scope[" << &rootScope << "]\n";
  2087. rootScope.locals.value[tLeft] = storage;
  2088. jsDisplayScope(&rootScope, 0);
  2089. left = ""; // reset left
  2090. assignfile << tLeft << "=" << it << "\n";
  2091. }
  2092. } else {
  2093. std::cout << std::string(doAssignmentLevel * 2, ' ') << "HALT can't get value from [" << it << "]\n";
  2094. }
  2095. }
  2096. } else if (state == 2) {
  2097. js_function *objectScope = new js_function;
  2098. js_object *newObject = new js_object;
  2099. //doAssignment(*objectScope, it);
  2100. parseJSON(*objectScope, it);
  2101. // translate the scope into js_object
  2102. //std::cout << "JSON object output" << std::endl;
  2103. for(auto it2 : objectScope->locals.value) {
  2104. //std::cout << "[" << it2.first << "=" << it2.second << "]" << std::endl;
  2105. newObject->value[it2.first] = it2.second;
  2106. }
  2107. //std::cout << "JSON object done" << std::endl;
  2108. std::string tLeft = trim(left);
  2109. assignfile << tLeft << "=" << "_NTRJSON" << "\n";
  2110. rootScope.locals.value[tLeft] = newObject;
  2111. state = 0;
  2112. } else if (state == 3) { // started with state 1 function (we're in a function definition)
  2113. // function body
  2114. //std::cout << "function body[" << it << "]\n";
  2115. //func = new js_function;
  2116. // parse params
  2117. //cursor = locateFunctionParamsEnd(it, 0);
  2118. // you need to find first brace
  2119. // call parseFunctionBody
  2120. //last = parseFunctionBody(it, cursor);
  2121. //parseFunctionBody(it, 0, *func);
  2122. // + 1 to skip initial { and not + 1 because }
  2123. //func = makeFunctionFromString(it.substr(cursor + 1, cursor - last), &rootScope);
  2124. //std::cout << "doAssignment makeFunctionFromString start[" << it[0] << "] last[" << it[it.length() - 1] << "]\n";
  2125. std::string body = it.substr(1, it.length() - 2);
  2126. //std::cout << "doAssignment makeFunctionFromString2 start[" << body[0] << "] last[" << body[body.length() - 1] << "]\n";
  2127. func = makeFunctionFromString(body, prototype, &rootScope);
  2128. // we already have the body of the function
  2129. // we need to get this token (string that is the body of the function)
  2130. // into js_function
  2131. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment - creating function [" << left << "] with [" << func->tokens.size() << "]tokens\n";
  2132. // warning copy & paste
  2133. std::string tLeft = trim(left);
  2134. size_t dotPos = tLeft.find('.');
  2135. bool alreadySet = false;
  2136. if (dotPos != std::string::npos) {
  2137. //std::cout << "doAssignment - Key Has . \n";
  2138. std::string baseObj = tLeft.substr(0, dotPos);
  2139. //std::cout << "doAssignment - base[" << baseObj << "]" << std::endl;
  2140. js_internal_storage *baseStorage = jsLocateKey(&rootScope, baseObj);
  2141. if (baseStorage) {
  2142. std::string part2 = tLeft.substr(dotPos + 1);
  2143. //std::cout << "doAssignment - part2[" << part2 << "]" << std::endl;
  2144. js_object *jobj = dynamic_cast<js_object*>(baseStorage);
  2145. if (jobj) {
  2146. /*
  2147. if (jobj->value.find(part2) != jobj->value.end()) {
  2148. std::cout << "doAssignment - part2[" << part2 << "] is inside base" << std::endl;
  2149. //storage = jobj->value[part2]; // we will now point to this storage
  2150. } else {
  2151. std::cout << "doAssignment - part2[" << part2 << "] not in base" << std::endl;
  2152. for(auto it2 : jobj->value) {
  2153. std::cout << "[" << it2.first << "]\n";
  2154. }
  2155. }
  2156. */
  2157. assignfile << tLeft << "=" << "_NTRFUNC" << "\n";
  2158. jobj->value[part2] = func;
  2159. alreadySet = true;
  2160. } else {
  2161. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment - baseStorage couldn't be made a js_object. type[" << typeOfStorage(baseStorage) << "]\n";
  2162. }
  2163. //
  2164. //js_internal_storage *p2Storage = locateKey(&rootScope, value);
  2165. } else {
  2166. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment - Couldnt find base[" << baseObj << "] in scope" << std::endl;
  2167. }
  2168. }
  2169. if (!alreadySet) {
  2170. rootScope.locals.value[tLeft] = func;
  2171. }
  2172. state = 5;
  2173. } else if (state == 4) {
  2174. if (it == "}") {
  2175. //displayScope(&rootScope, 0);
  2176. state = 5; // reset state
  2177. }
  2178. } else if (state == 5) { // function definition call check
  2179. // wait for ()
  2180. if (it == "(") {
  2181. state = 6;
  2182. } else {
  2183. state = 0; // reset
  2184. }
  2185. } else if (state == 6) { // call function just defined
  2186. if (it == ")") {
  2187. // parse/execute the tokens of said function
  2188. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment6 - calling just defined function, tokens[" << func->tokens.size() << "]\n";
  2189. if (func->tokens.size()) {
  2190. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment6 - calling just defined function, first token[" << func->tokens[0] << "]\n";
  2191. }
  2192. jsParseTokens(func->tokens, func);
  2193. state = 0; // reset
  2194. } else {
  2195. // collect params
  2196. std::cout << std::string(doAssignmentLevel * 2, ' ') << "doAssignment6 - WRITE ME collect params [" << it << "]\n";
  2197. }
  2198. } else if (state == 8) { // is a function call or expression
  2199. if (it == ")") {
  2200. state = 0; // reset state
  2201. } else {
  2202. // call params
  2203. std::cout << std::string(doAssignmentLevel * 2, ' ') << "HALT need to parse these params[" << it << "]\n";
  2204. }
  2205. } else if (state == 9) { // are we a function call with return value, or assigning a function reference
  2206. js_internal_storage *storage = nullptr;
  2207. // we'll need a way to get tLeft back to set it
  2208. if (it == "(") {
  2209. // it's a function call
  2210. // set state = 10 until we get all prototype
  2211. state = 10;
  2212. //storage = jsParseTokens(callFunc->tokens, callFunc);
  2213. } else {
  2214. // it's a function reference
  2215. storage = callFunc;
  2216. state = 0; // assuming end of this expression
  2217. }
  2218. if (storage) {
  2219. std::string tLeft = trim(left);
  2220. if (tLeft[0]=='"' && tLeft[tLeft.length() - 1]=='"') {
  2221. //std::cout << "dequoting[" << tLeft << "]" << std::endl;
  2222. tLeft=tLeft.substr(1, tLeft.length() - 2);
  2223. }
  2224. bool alreadySet = false;
  2225. size_t dotPos = tLeft.find('.');
  2226. if (dotPos != std::string::npos) {
  2227. js_internal_storage **key = getObjectKeyPointer(tLeft, &rootScope);
  2228. if (key != nullptr) {
  2229. alreadySet = true;
  2230. *key = storage;
  2231. }
  2232. }
  2233. if (!alreadySet) {
  2234. std::cout << "doAssignment final assign[" << tLeft << "] of type[" << typeOfStorage(storage) << "] scope[" << &rootScope << "]\n";
  2235. rootScope.locals.value[tLeft] = storage;
  2236. jsDisplayScope(&rootScope, 0);
  2237. left = ""; // reset left
  2238. assignfile << tLeft << "=" << it << "\n";
  2239. }
  2240. }
  2241. } else if (state == 10) { // in function call params
  2242. if (it == ")") {
  2243. // use doFunctionCall
  2244. js_internal_storage *storage = nullptr;
  2245. std::cout << "calling function [" << callFunc << "](" << params << ")\n";
  2246. storage = doFunctionCall(callFunc, params, rootScope);
  2247. std::cout << "function retval[" << storage << "] type[" << typeOfStorage(storage) << "]\n";
  2248. if (storage) {
  2249. std::string tLeft = trim(left);
  2250. if (tLeft[0]=='"' && tLeft[tLeft.length() - 1]=='"') {
  2251. //std::cout << "dequoting[" << tLeft << "]" << std::endl;
  2252. tLeft=tLeft.substr(1, tLeft.length() - 2);
  2253. }
  2254. bool alreadySet = false;
  2255. size_t dotPos = tLeft.find('.');
  2256. if (dotPos != std::string::npos) {
  2257. js_internal_storage **key = getObjectKeyPointer(tLeft, &rootScope);
  2258. if (key != nullptr) {
  2259. alreadySet = true;
  2260. *key = storage;
  2261. }
  2262. }
  2263. if (!alreadySet) {
  2264. std::cout << "doAssignment final assign[" << tLeft << "] of type[" << typeOfStorage(storage) << "] scope[" << &rootScope << "]\n";
  2265. rootScope.locals.value[tLeft] = storage;
  2266. jsDisplayScope(&rootScope, 0);
  2267. left = ""; // reset left
  2268. assignfile << tLeft << "=" << it << "\n";
  2269. }
  2270. }
  2271. state = 0;
  2272. } else {
  2273. // collect params
  2274. params = it;
  2275. }
  2276. }
  2277. // { starts JSON capture (should be exactly one block before the } token)
  2278. // you create a scope for that variable and recurse
  2279. lastToken = it;
  2280. }
  2281. std::cout << std::string(doAssignmentLevel * 2, ' ') << "expression end, state " << std::to_string(state) << std::endl << std::endl;
  2282. //displayScope(&rootScope, 0);
  2283. /*
  2284. auto hasTripleEqual = token.find("===");
  2285. auto hasDoubleEqual = std::string::npos;
  2286. auto hasSingleEqual = std::string::npos;
  2287. if (hasTripleEqual == std::string::npos) {
  2288. hasDoubleEqual = token.find("==");
  2289. } else {
  2290. // process === expression
  2291. std::cout << "JSParser:::doAssignment - strict compare not implemented" << std::endl;
  2292. //std::cout << "token[" << token << "]" << std::endl;
  2293. }
  2294. if (hasDoubleEqual == std::string::npos) {
  2295. hasSingleEqual = token.find("=");
  2296. } else {
  2297. // process == expression
  2298. std::cout << "JSParser:::doAssignment - compare not implemented" << std::endl;
  2299. }
  2300. if (hasSingleEqual != std::string::npos) {
  2301. auto keyValue = split(token, '=');
  2302. if (keyValue.size() < 2) {
  2303. std::cout << "JSParser:::doAssignment - bad var parse " << keyValue[0] << std::endl;
  2304. return false;
  2305. }
  2306. // FIXME: dot notation in keys
  2307. auto key = trim(keyValue[0]);
  2308. // FIXME: is value a lambda
  2309. auto value = trim(keyValue[1]);
  2310. //std::cout << "[" << key << "=" << value << "]" << std::endl;
  2311. rootScope.variables[key] = value;
  2312. } else {
  2313. // var bob; just make sure the variable exists
  2314. rootScope.variables[token] = "";
  2315. }
  2316. */
  2317. doAssignmentLevel--;
  2318. return true;
  2319. }
  2320. js_internal_storage **getObjectKeyPointer(const std::string input, const js_function *scope) {
  2321. size_t dotPos = input.find('.');
  2322. if (dotPos != std::string::npos) {
  2323. std::cout << "getObjectKeyPointer - Key Has . scope[" << scope << "]\n";
  2324. std::string baseObj = input.substr(0, dotPos);
  2325. std::cout << "getObjectKeyPointer - base[" << baseObj << "]" << std::endl;
  2326. js_internal_storage *baseStorage = jsLocateKey(scope, baseObj);
  2327. if (baseStorage) {
  2328. std::string part2 = input.substr(dotPos + 1);
  2329. js_object *jobj = dynamic_cast<js_object*>(baseStorage);
  2330. // if not object, try ref and func
  2331. if (jobj == nullptr) {
  2332. js_reference *jref = dynamic_cast<js_reference*>(baseStorage);
  2333. if (jref) {
  2334. // try to cast it as an object
  2335. jobj = dynamic_cast<js_object*>(jref->ptr);
  2336. if (jobj == nullptr) {
  2337. // if not an object, try function
  2338. js_function *jfunc = dynamic_cast<js_function*>(jref->ptr);
  2339. if (jfunc) {
  2340. jobj = &jfunc->locals;
  2341. }
  2342. }
  2343. }
  2344. }
  2345. if (jobj) {
  2346. //jobj->value[part2] = storage;
  2347. //assignfile << "[" << baseObj << "].[" << part2 << "]=" << "\n";
  2348. //return true;
  2349. /*
  2350. std::cout << "getObjectKeyPointer - precheck\n";
  2351. for(auto it2 : jobj->value) {
  2352. std::cout << "getObjectKeyPointer - precheck key[" << it2.first << "] valueType [" << typeOfStorage(it2.second) << "]\n";
  2353. }
  2354. */
  2355. std::cout << "getObjectKeyPointer found base value[" << jobj->value[part2] << "] type[" << typeOfStorage(jobj->value[part2]) << "]\n";
  2356. if (jobj->value[part2] == nullptr) {
  2357. std::cout << "getObjectKeyPointer - NULL value\n";
  2358. for(auto it2 : jobj->value) {
  2359. std::string type = typeOfStorage(it2.second);
  2360. if (type == "string") {
  2361. js_string *string = dynamic_cast<js_string*>(it2.second);
  2362. std::cout << "getObjectKeyPointer - NULL key[" << it2.first << "] address[" << it2.second << "] stringValue[" << string->value << "]\n";
  2363. } else {
  2364. std::cout << "getObjectKeyPointer - NULL key[" << it2.first << "] address[" << it2.second << "] valueType [" << type << "]\n";
  2365. }
  2366. }
  2367. }
  2368. return &jobj->value[part2];
  2369. } else {
  2370. std::cout << "getObjectKeyPointer [" << baseObj << "] isnt an object\n";
  2371. }
  2372. } else {
  2373. std::cout << "getObjectKeyPointer - Couldnt find base[" << baseObj << "] in scope" << std::endl;
  2374. jsDisplayScope(scope, 0);
  2375. }
  2376. } else {
  2377. // still check our scope for
  2378. }
  2379. return nullptr;
  2380. }
  2381. js_internal_storage *dereferenceObject(const std::string input, const js_function *scope) {
  2382. // FIXME: too simple, catches quoted strings with . in them
  2383. // FIXME: make sure we're not inside quotes
  2384. size_t dotPos = input.find('.');
  2385. if (dotPos != std::string::npos) {
  2386. std::string baseObj = input.substr(0, dotPos);
  2387. js_internal_storage *baseStorage = jsLocateKey(scope, baseObj);
  2388. if (baseStorage) {
  2389. std::string part2 = input.substr(dotPos + 1);
  2390. js_object *jobj = dynamic_cast<js_object*>(baseStorage);
  2391. if (jobj) {
  2392. if (jobj->value.find(part2) != jobj->value.end()) {
  2393. std::cout << "dereferenceObject - part2[" << part2 << "] is inside base" << std::endl;
  2394. return jobj->value[part2]; // we will now point to this storage
  2395. } else {
  2396. std::cout << "dereferenceObject - part2[" << part2 << "] not in base" << std::endl;
  2397. for(auto it2 : jobj->value) {
  2398. std::cout << "[" << it2.first << "]\n";
  2399. }
  2400. }
  2401. } else {
  2402. std::cout << "dereferenceObject - baseStorage couldn't be made a js_object. type[" << typeOfStorage(baseStorage) << "]\n";
  2403. }
  2404. } else {
  2405. std::cout << "dereferenceObject - Couldnt find base[" << baseObj << "] in scope" << std::endl;
  2406. }
  2407. }
  2408. dotPos = input.find('[');
  2409. if (dotPos != std::string::npos) {
  2410. size_t end = input.find(']');
  2411. if (end != std::string::npos) {
  2412. // we have an open and close
  2413. std::string baseObj = input.substr(0, dotPos);
  2414. js_internal_storage *baseStorage = jsLocateKey(scope, baseObj);
  2415. if (baseStorage) {
  2416. std::string part2 = input.substr(dotPos + 1, end - dotPos - 1);
  2417. char lastChar = part2[part2.length() - 1];
  2418. // probably should run this through doExpression...
  2419. // FIXME: "asdf" + var + "zxcv"
  2420. if ((part2[0] == '"' && lastChar == '"') || (part2[0] == '\'' && lastChar == '\'')) {
  2421. js_object *jobj = dynamic_cast<js_object*>(baseStorage);
  2422. if (jobj) {
  2423. std::cout << "dereferenceObject bracket style <" << baseObj << ">[<" << part2 << "><" << input.substr(end) << ">\n";
  2424. // is constant
  2425. std::string constant = part2.substr(1, part2.length() - 2);
  2426. std::cout << "constant[" << constant << "]\n";
  2427. if (jobj->value.find(constant) != jobj->value.end()) {
  2428. std::cout << "dereferenceObject - constant[" << constant << "] is inside base" << std::endl;
  2429. return jobj->value[part2]; // we will now point to this storage
  2430. } else {
  2431. std::cout << "dereferenceObject - constant[" << constant << "] not in base" << std::endl;
  2432. for(auto it2 : jobj->value) {
  2433. std::cout << "[" << it2.first << "]\n";
  2434. }
  2435. }
  2436. } else {
  2437. std::cout << "dereferenceObject - baseStorage[" << baseObj << "] couldn't be made a js_object. type[" << typeOfStorage(baseStorage) << "]\n";
  2438. if (typeOfStorage(baseStorage) == "string") {
  2439. js_string *jstring = dynamic_cast<js_string *>(baseStorage);
  2440. std::cout << "baseStorage string value[" << jstring->value << "]\n";
  2441. }
  2442. }
  2443. } else {
  2444. // is variable
  2445. std::cout << "dereferenceObject bracket style HALT, expression dereference\n";
  2446. }
  2447. } else {
  2448. std::cout << "dereferenceObject - Couldnt find base[" << baseObj << "] in scope" << std::endl;
  2449. }
  2450. }
  2451. }
  2452. return nullptr;
  2453. }
  2454. // should return if we're halted or not...
  2455. js_internal_storage *jsParseTokens(const std::vector<std::string> &tokens, js_function *scope) {
  2456. // we need to at least build the root scope
  2457. //std::cout << "\nstart script" << std::endl;
  2458. js_internal_storage *stack = nullptr;
  2459. for(auto it : tokens) {
  2460. std::string ttoken = trim(it);
  2461. if (ttoken == "") continue; // skip empty tokens
  2462. std::cout << "parse token[" << it << "]" << std::endl;
  2463. if (ttoken.substr(0, 2)=="if") {
  2464. execfile << "if not implemented\n";
  2465. std::string ifStr = it.substr(2);
  2466. std::cout << "ifStr1[" << ifStr << "]" << std::endl;
  2467. // find (
  2468. size_t end = ifStr.find('(');
  2469. ifStr = ifStr.substr(end + 1); // skip the (
  2470. std::cout << "ifStr2[" << ifStr << "]" << std::endl;
  2471. // find )
  2472. // too simple, won't work if expression has a function call...
  2473. end = getNextExpression(ifStr, 0); // works great so far
  2474. //end = ifStr.find(')');
  2475. std::string ifCondition = ifStr.substr(0, end);
  2476. js_internal_storage *expRes = doExpression(*scope, ifCondition);
  2477. bool conditionRes = jsIsFalse(expRes);
  2478. std::cout << "ifCondition[" << ifCondition << "] res[" << conditionRes << "]" << std::endl;
  2479. ifStr = ifStr.substr(0, end);
  2480. // then we'll use !isFalse to figure out if we need to exec this attached scope of code
  2481. // also don't forget about the else block and potentially elif
  2482. // FIXME: do we have a block start? yes but we won't have the other lines....
  2483. std::cout << "true block start search[" << ttoken.substr(3 + ifCondition.size()) << "]" << std::endl;
  2484. size_t start = ttoken.substr(3 + ifCondition.size()).find(')') + 3 + ifCondition.size() + 1; // after the )
  2485. end = parseFunctionBody(ttoken, start);
  2486. std::cout << "true block[" << ttoken.substr(start, end - start) << "]" << std::endl;
  2487. if (conditionRes) {
  2488. // a return in an if scope, would return from the function it's in
  2489. std::vector<std::string> tokens = jsGetTokens(ttoken, start);
  2490. js_internal_storage *val=jsParseTokens(tokens, scope);
  2491. std::cout << "value res[" << val << "]\n";
  2492. // skip all elses
  2493. } else {
  2494. // handle else
  2495. std::cout << "looking for else [" << ttoken.substr(end) << "]\n";
  2496. start = ttoken.substr(end).find("else");
  2497. if (start != std::string::npos) {
  2498. std::cout << "false condition ELSE not implemented" << std::endl;
  2499. }
  2500. }
  2501. //std::cout << "HALT if not implemented" << std::endl;
  2502. //return nullptr;
  2503. } else if (ttoken.substr(0, 3)=="var") {
  2504. std::string listStr = it.substr(3);
  2505. // FIXME: , in quotes or {} (JSON) <= top priority for 4chan
  2506. std::vector<std::string> opens, closes;
  2507. opens.push_back("{");
  2508. opens.push_back("'");
  2509. opens.push_back("\"");
  2510. closes.push_back("}");
  2511. closes.push_back("'");
  2512. closes.push_back("\"");
  2513. auto varList = parseSepButNotBetween(listStr, ",", opens, closes);
  2514. //std::cout << "has " << varList.size() << " variables" << std::endl;
  2515. for(auto it2 : varList) {
  2516. //std::cout << "var processing [" << it2 << "]" << std::endl;
  2517. /*
  2518. // FIXME: make sure = isn't in quotes or JSON?
  2519. // FIXME: double or triple equal differentiation
  2520. //std::cout << "looking at [" << it2 << "]" << std::endl;
  2521. auto hasTripleEqual = it2.find("===");
  2522. auto hasDoubleEqual = std::string::npos;
  2523. auto hasSingleEqual = std::string::npos;
  2524. if (hasTripleEqual == std::string::npos) {
  2525. hasDoubleEqual = it2.find("==");
  2526. } else {
  2527. // process expression
  2528. std::cout << "var strict compare not implemented" << std::endl;
  2529. }
  2530. if (hasDoubleEqual == std::string::npos) {
  2531. hasSingleEqual = it2.find("=");
  2532. } else {
  2533. // process expression
  2534. std::cout << "var compare not implemented" << std::endl;
  2535. }
  2536. if (hasSingleEqual != std::string::npos) {
  2537. auto keyValue = split(it2, '=');
  2538. if (keyValue.size() < 2) {
  2539. std::cout << "bad var parse " << keyValue[0] << std::endl;
  2540. continue;
  2541. }
  2542. // FIXME: dot notation in keys
  2543. auto key = trim(keyValue[0]);
  2544. auto value = trim(keyValue[1]);
  2545. //std::cout << "[" << key << "=" << value <<s"]" << std::endl;
  2546. script->rootScope.variables[key] = value;
  2547. } else {
  2548. // var bob; just make sure the variable exists
  2549. script->rootScope.variables[it2] = "";
  2550. }
  2551. */
  2552. //std::cout << "About to assign, current scope: \n";
  2553. //displayScope(scope, 0);
  2554. execfile << "var - " << trim(it2) << " scope[" << scope << "]\n";
  2555. doAssignment(*scope, it2);
  2556. }
  2557. } else if (ttoken.substr(0, 9)=="function ") {
  2558. // ParseFunction
  2559. std::string defStr = it.substr(9);
  2560. // find ( (name end, prototype start)
  2561. size_t end = defStr.find('(');
  2562. std::string funcName = defStr.substr(0, end);
  2563. defStr = defStr.substr(end + 1); // next char after (
  2564. // find ) (prototype end)
  2565. end = defStr.find(')');
  2566. std::string prototype = defStr.substr(0, end);
  2567. defStr = defStr.substr(end + 1); // next char after )
  2568. // find { (func start)
  2569. end = defStr.find('{');
  2570. defStr = defStr.substr(end + 1, defStr.size() - 2); // from { to the end
  2571. //std::cout << "jsParseTokens Function Declartion start[" << defStr[0] << "] last[" << defStr[defStr.length() - 1] << "]\n";
  2572. auto funcTokens = jsGetTokens(defStr, 0);
  2573. //std::cout << "function [" << funcName << "] prototype [" << prototype << "] has [" << funcTokens.size() << "] tokens" << std::endl;
  2574. // __netrunner_function_definition is 31 chars
  2575. //scope->variables[funcName] = "__netrunner_function_definition = { prototype: \"" + prototype + "\", code: \"" + defStr + "\" }";
  2576. js_function *newFunc = new js_function;
  2577. newFunc->tokens = funcTokens;
  2578. newFunc->parentScope = scope;
  2579. scope->locals.value[funcName] = newFunc;
  2580. execfile << "function declaration [" << funcName << "](" << prototype << ") tokens[" << funcTokens.size() << "]\n";
  2581. } else if (ttoken.substr(0, 6)=="return") {
  2582. execfile << "return not implemented\n";
  2583. // js expression here
  2584. // probably don't need to do anything here atm
  2585. std::cout << "HALT return not implemented" << std::endl;
  2586. return nullptr;
  2587. } else if (ttoken.find("=") != std::string::npos) {
  2588. execfile << "assignment - " << it << "\n";
  2589. // has = so it's an expression
  2590. //std::cout << "assignment[" << it << "]" << std::endl;
  2591. //std::cout << "assignment not implemented" << std::endl;
  2592. doAssignment(*scope, it);
  2593. } else if (ttoken.find("(") != std::string::npos && ttoken.find(")") != std::string::npos) {
  2594. execfile << "function call\n";
  2595. // has () so it's a function call
  2596. //std::cout << "funcCall[" << it << "]" << std::endl;
  2597. // we need to start passed any && or ||
  2598. // need to parse any expression before the function call...
  2599. if (it.find("&&") == std::string::npos && it.find("||") == std::string::npos) {
  2600. // figure out function name
  2601. size_t parenStart = it.find("(");
  2602. std::string funcName = it.substr(0, parenStart);
  2603. //std::cout << "I think the function name is [" << funcName << "]" << std::endl;
  2604. std::string arguments = it.substr(parenStart + 1, it.find(")") - parenStart - 1);
  2605. //std::cout << "functionCall[" << funcName << "](" << arguments << ") not implemented" << std::endl;
  2606. execfile << "function call [" << funcName << "](" << arguments << ")\n";
  2607. js_internal_storage *storage = nullptr;
  2608. storage = dereferenceObject(funcName, scope);
  2609. // need to convert to storage to create js_function object
  2610. if (storage == nullptr) {
  2611. storage = jsLocateKey(scope, funcName);
  2612. }
  2613. if (storage == nullptr) {
  2614. std::cout << "HALT Function [" << funcName << "] d.n.e in this scope or parents, creating forwardCall" << std::endl;
  2615. jsDisplayScope(scope, 0);
  2616. return nullptr;
  2617. //scope->forwardCalls.push_back(funcName);
  2618. // we can't do much without the body of the function
  2619. } else {
  2620. js_function *func = dynamic_cast<js_function *>(storage);
  2621. if (!func) {
  2622. std::cout << "HALT Function [" << funcName << "] data isn't a function" << std::endl;
  2623. return nullptr;
  2624. }
  2625. // make sure function is parsed
  2626. // and step through tokens
  2627. // we should update the parameter values...
  2628. std::cout << "WARNING functionCall[" << funcName << "](" << arguments << ") parameters not implemented" << std::endl;
  2629. jsParseTokens(func->tokens, func);
  2630. //std::cout << "parameters[" << arguments << "]" << std::endl;
  2631. }
  2632. } else {
  2633. execfile << "function call - expression scope[" << scope << "]\n";
  2634. // we have an || or && instead ()
  2635. // window.clickable_ids&&document.addEventListener("4chanParsingDone",onParsingDone,!1)
  2636. // well it's technically an expression
  2637. // as long as we handle the && first
  2638. // then the left side before the right
  2639. // well fuck, lets tokenize left to right
  2640. stack = doExpression(*scope, it);
  2641. //std::cout << "HALT expression before functionCall not implemented [" << it << "]" << std::endl;
  2642. //return nullptr;
  2643. }
  2644. //std::cout << "functionCall not implemented" << std::endl;
  2645. } else {
  2646. std::cout << "jsParseTokens - unknown_type[" << it << "]" << std::endl;
  2647. }
  2648. }
  2649. return stack;
  2650. }
  2651. void JavaScript::parse(const std::string source) {
  2652. // tokenize source
  2653. //std::cout << "JavaScript::parse source[" << source << "]\n\n";
  2654. this->tokens = jsGetTokens(source, 0);
  2655. jsParseTokens(this->tokens, &this->rootScope);
  2656. }
  2657. void JavaScript::execute() {
  2658. // probably not needed because JS is executed as parsed in the browser
  2659. }
  2660. void JavaScript::append(const std::shared_ptr<JavaScript> &source) {
  2661. for(auto it : source->tokens) {
  2662. this->tokens.push_back(it);
  2663. }
  2664. }
  2665. void JavaScript::applyScope(const std::shared_ptr<JavaScript> &source) {
  2666. // merge scopes (instead of reparsing)
  2667. //std::cout << "JavaScript::append - merge scope" << std::endl;
  2668. for(auto it : source->rootScope.locals.value) {
  2669. //std::cout << "JavaScript::append - copying " << it.first << std::endl;
  2670. std::string type = typeOfStorage(it.second);
  2671. if (type == "reference") {
  2672. js_reference *srcRefDest = dynamic_cast<js_reference*>(it.second);
  2673. if (srcRefDest != nullptr) {
  2674. /*
  2675. bool found = false;
  2676. std::string sourceKey = "";
  2677. for(auto it2 : source->rootScope.locals.value) {
  2678. std::cout << "ptr[" << srcRefDest->ptr << "]==[" << it2.second << "]\n";
  2679. if (it2.second == srcRefDest->ptr) {
  2680. sourceKey = it2.first;
  2681. found = true;
  2682. break;
  2683. }
  2684. }
  2685. */
  2686. if (srcRefDest->ptr == &source->rootScope) {
  2687. //std::cout << "JavaScript::append - copying reference " << it.first << " points to root" << std::endl;
  2688. js_reference *trgRef = new js_reference; // FIXME: clean up somewhere
  2689. trgRef->ptr = &this->rootScope;
  2690. this->rootScope.locals.value[it.first] = trgRef;
  2691. } else {
  2692. std::string sourceKey = jsLocatePtrKey(&source->rootScope, srcRefDest->ptr);
  2693. if (sourceKey != "") {
  2694. std::cout << "JavaScript::append - WRITE ME copying reference " << it.first << " points to sourceKey[" << sourceKey << "] type[" << typeOfStorage(srcRefDest->ptr) << "]" << std::endl;
  2695. } else {
  2696. std::cout << "JavaScript::append - Reference[" << it.first << "] couldnt find reference\n";
  2697. }
  2698. }
  2699. } else {
  2700. std::cout << "JavaScript::append - Reference[" << it.first << "] doesnt point to a reference\n";
  2701. }
  2702. } else {
  2703. this->rootScope.locals.value[it.first] = it.second;
  2704. }
  2705. }
  2706. //std::cout << "JavaScript::append - after scope" << std::endl;
  2707. //jsDisplayScope(&this->rootScope, 0);
  2708. }