From 25420ebb21a2d4f78bcbb4868c932591dba1805e Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 12 Jul 2019 18:41:09 +0900 Subject: [PATCH] support global variable for console and document --- examples/array.cc | 24 ++++++++++++------------ examples/image.cc | 12 +++++------- examples/promise.cc | 14 +++++++------- examples/string.cc | 2 +- include/array.h | 2 +- include/export.h | 2 +- include/libhtml5.h | 2 ++ src/export.cc | 7 +++++++ 8 files changed, 36 insertions(+), 29 deletions(-) diff --git a/examples/array.cc b/examples/array.cc index ca23822..e1c73e3 100644 --- a/examples/array.cc +++ b/examples/array.cc @@ -7,47 +7,47 @@ static void arrayTest() html5::array array = { 1, M_PI, "hello", subarray, html5::Object::create(), }; - html5::window->console->log("array = ", array); + html5::console->log("array = ", array); int ivalue = array[0]; - html5::window->console->log("array[0] = ", ivalue); + html5::console->log("array[0] = ", ivalue); double fvalue = array[1]; - html5::window->console->log("array[1] = ", fvalue); + html5::console->log("array[1] = ", fvalue); std::string svalue = array[2]; - html5::window->console->log("array[2] = ", svalue); + html5::console->log("array[2] = ", svalue); html5::array *avalue = array[3]; - html5::window->console->log("array[3] = ", avalue); + html5::console->log("array[3] = ", avalue); html5::Object *ovalue = array[4]; - html5::window->console->log("array[4] = ", ovalue); + html5::console->log("array[4] = ", ovalue); int index = 0; for (const auto &e : array) { switch (index) { case 0: - html5::window->console->log((int)e); + html5::console->log((int)e); break; case 1: - html5::window->console->log((double)e); + html5::console->log((double)e); break; case 2: - html5::window->console->log((std::string)e); + html5::console->log((std::string)e); break; case 3: - html5::window->console->log((html5::array)e); + html5::console->log((html5::array)e); break; case 4: - html5::window->console->log((html5::Object *)e); + html5::console->log((html5::Object *)e); break; } index++; } array.forEach([](const html5::array::Element &e, int index){ - html5::window->console->log(index); + html5::console->log(index); }); } diff --git a/examples/image.cc b/examples/image.cc index e14c6df..872e05e 100644 --- a/examples/image.cc +++ b/examples/image.cc @@ -3,22 +3,20 @@ static void createImage(std::string url) { - for (html5::StyleSheet *sheet : *html5::window->document->styleSheets) { - html5::window->console->log(sheet); + for (html5::StyleSheet *sheet : *html5::document->styleSheets) { + html5::console->log(sheet); } html5::HTMLImageElement *image = html5::HTMLImageElement::create(); static html5::EventHandler onload = [image](html5::Event *e){ std::cout << "callback. onload" << std::endl; - html5::window->console->trace(); + html5::console->trace(); std::cout << "width = " << image->width << std::endl; std::cout << "height = " << image->height << std::endl; std::cout << "naturalWidth = " << image->naturalWidth << std::endl; std::cout << "naturalHeight = " << image->naturalHeight << std::endl; }; - html5::Console *console = html5::window->console; image->onload = &onload; image->src = url; - html5::Document *document = html5::window->document; - document->body->appendChild(image); - console->log("image = ", image); + html5::document->body->appendChild(image); + html5::console->log("image = ", image); } diff --git a/examples/promise.cc b/examples/promise.cc index 1a2ede3..fa1135e 100644 --- a/examples/promise.cc +++ b/examples/promise.cc @@ -3,16 +3,16 @@ static void promiseTest() { auto promise = html5::Promise::create([](std::function resolve, std::function reject) { - html5::window->setTimeout([resolve](){ resolve("foo"); }, 300); + html5::setTimeout([resolve](){ resolve("foo"); }, 300); }); promise->then([](const std::string &value) { - html5::window->console->log(value); + html5::console->log(value); return html5::Promise::resolve("bar"); })->then([](const std::string &value) { - html5::window->console->log(value); + html5::console->log(value); return html5::Promise::resolve(0.0); }); - html5::window->console->log(promise); + html5::console->log(promise); } static void fetchImageTest(const std::string &imageURL) @@ -25,15 +25,15 @@ static void fetchImageTest(const std::string &imageURL) std::string data = reader->result; auto image = html5::HTMLImageElement::create(); image->src = data; - html5::window->document->body->appendChild(image); + html5::document->body->appendChild(image); }; reader->readAsDataURL(blob); return nullptr; })->catchError([]{ - html5::window->console->log("catch"); + html5::console->log("catch"); return nullptr; })->finally([]{ - html5::window->console->log("finally"); + html5::console->log("finally"); return nullptr; }); } diff --git a/examples/string.cc b/examples/string.cc index fb8ef83..a4569a3 100644 --- a/examples/string.cc +++ b/examples/string.cc @@ -5,7 +5,7 @@ static void stringTest() { html5::string s = "hello world"; std::cout << s << std::endl; - html5::window->console->log(s); + html5::console->log(s); } { std::string s = "hello"; diff --git a/include/array.h b/include/array.h index 695b62a..c8b89c3 100644 --- a/include/array.h +++ b/include/array.h @@ -62,7 +62,7 @@ class array : public Object { iterator end() { return iterator(this, this->length); }; array(emscripten::val v); - template array(const Args&... args) : Object(HTML5_NEW_PRIMITIVE_INSTANCE(array)) { + template array(const Args&... args) : Object(HTML5_NEW_PRIMITIVE_INSTANCE(Array)) { push(args...); }; virtual ~array(); diff --git a/include/export.h b/include/export.h index 95bd665..17fa4e9 100644 --- a/include/export.h +++ b/include/export.h @@ -59,7 +59,7 @@ extern void print(); extern std::string prompt(std::string message, std::string _default); extern long requestAnimationFrame(std::function *callback); extern long setInterval(std::function *handler, long timeout); -extern long setTimeout(std::function *handler, long timeout); +extern long setTimeout(std::function handler, long timeout); extern void stop(); NAMESPACE_HTML5_END; diff --git a/include/libhtml5.h b/include/libhtml5.h index 0587a2b..4ea8e2a 100644 --- a/include/libhtml5.h +++ b/include/libhtml5.h @@ -6,5 +6,7 @@ NAMESPACE_HTML5_BEGIN; extern Window *window; +extern Console *console; +extern Document *document; NAMESPACE_HTML5_END; diff --git a/src/export.cc b/src/export.cc index 7375f91..262b221 100644 --- a/src/export.cc +++ b/src/export.cc @@ -1,9 +1,12 @@ #include "libhtml5.h" #include "window.h" +#include "console.h" NAMESPACE_HTML5_BEGIN; Window *window; +Console *console; +Document *document; static bool g_initialized = false; @@ -14,6 +17,10 @@ class __runtime__ { window = Window::create(); window->retain(); + console = window->console; + console->retain(); + document = window->document; + document->retain(); g_initialized = true; } };