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.

BrowserJS.cpp 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "BrowserJS.h"
  2. std::vector<std::string> getConstructs() {
  3. std::vector<std::string> constructs;
  4. constructs.push_back("document.getElementsByTagName");
  5. constructs.push_back("document.addEventListener");
  6. constructs.push_back("document.querySelector");
  7. constructs.push_back("console.log");
  8. return constructs;
  9. }
  10. bool isConstruct(std::string construct) {
  11. std::vector<std::string> constructs = getConstructs();
  12. std::vector<std::string>::iterator it = std::find(constructs.begin(), constructs.end(), construct);
  13. std::cout << "isConstruct [" << construct << "](" << ((it != constructs.end())?"true":"false") << ")\n";
  14. return it != constructs.end();
  15. }
  16. DocumentComponent *getDocumentFromScope(js_function &scope) {
  17. if (scope.parentScope) {
  18. return getDocumentFromScope(*scope.parentScope);
  19. }
  20. // no more parent
  21. if (!scope.script) {
  22. std::cout << "getDocumentFromScope couldnt find doc, no script\n";
  23. return nullptr;
  24. }
  25. BrowserJavaScript *bScript = dynamic_cast<BrowserJavaScript *>(scope.script);
  26. if (!bScript)
  27. {
  28. std::cout << "getDocumentFromScope rootScript isnt a browserJS\n";
  29. return nullptr;
  30. }
  31. return bScript->document;
  32. }
  33. js_internal_storage *executeConstruct(std::string functionName, std::string params, js_function &scope) {
  34. std::vector<std::string> constructs = getConstructs();
  35. std::vector<std::string>::iterator it = std::find(constructs.begin(), constructs.end(), functionName);
  36. if (it == constructs.end()) {
  37. return nullptr;
  38. }
  39. std::cout << "executeConstruct [" << functionName << "](" << params << ")\n";
  40. if (functionName == "document.querySelector") {
  41. return jsConstruct_querySelector(params, scope);
  42. }
  43. if (functionName == "document.addEventListener") {
  44. return jsConstruct_addEventListener(params, scope);
  45. }
  46. if (functionName == "document.getElementsByTagName") {
  47. return jsConstruct_getElementsByTagName(params, scope);
  48. }
  49. std::cout << "executeConstruct handler needs to be written\n";
  50. return nullptr;
  51. }
  52. js_internal_storage *jsConstruct_querySelector(std::string params, js_function &scope) {
  53. js_bool *jb = new js_bool;
  54. jb->value = false;
  55. return jb;
  56. }
  57. js_internal_storage *jsConstruct_addEventListener(std::string params, js_function &scope) {
  58. std::cout << "WARNING implement jsConstruct_addEventListener\n";
  59. js_bool *jb = new js_bool;
  60. jb->value = false;
  61. return jb;
  62. }
  63. js_internal_storage *jsConstruct_getElementsByTagName(std::string params, js_function &scope) {
  64. //std::cout << "WARNING implement jsConstruct_getElementsByTagName\n";
  65. js_array *ja = new js_array;
  66. // how do we access the DOM?
  67. DocumentComponent *doc = getDocumentFromScope(scope);
  68. if (!doc) {
  69. return ja; // return empty array
  70. }
  71. // iterate down (maybe flush tree to a list)
  72. std::vector< std::shared_ptr<Node> > results;
  73. Node::findTagNodeChild(params, doc->domRootNode, results);
  74. std::cout << "Found [" << results.size() << "] for [" << params << "]\n";
  75. for(auto foundTag : results) {
  76. //TagNode *tag = dynamic_cast<TagNode *>(foundTag.get());
  77. js_element *je = new js_element;
  78. je->value = foundTag.get();
  79. ja->value.push_back(je);
  80. }
  81. std::cout << "Found [" << ja->value.size() << "] of [" << params << "]\n";
  82. // ja->value is a vector of js_internal_storage
  83. return ja;
  84. }