Skip to content

Commit

Permalink
support global variable for console and document
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Jul 12, 2019
1 parent 75f2c83 commit 25420eb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 29 deletions.
24 changes: 12 additions & 12 deletions examples/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down
12 changes: 5 additions & 7 deletions examples/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
14 changes: 7 additions & 7 deletions examples/promise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
static void promiseTest()
{
auto promise = html5::Promise::create([](std::function<void(const std::string &)> resolve, std::function<void()> 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)
Expand All @@ -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;
});
}
Expand Down
2 changes: 1 addition & 1 deletion examples/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
2 changes: 1 addition & 1 deletion include/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class array : public Object {
iterator end() { return iterator(this, this->length); };

array(emscripten::val v);
template<typename... Args> array(const Args&... args) : Object(HTML5_NEW_PRIMITIVE_INSTANCE(array)) {
template<typename... Args> array(const Args&... args) : Object(HTML5_NEW_PRIMITIVE_INSTANCE(Array)) {
push(args...);
};
virtual ~array();
Expand Down
2 changes: 1 addition & 1 deletion include/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extern void print();
extern std::string prompt(std::string message, std::string _default);
extern long requestAnimationFrame(std::function<void(double)> *callback);
extern long setInterval(std::function<void(void)> *handler, long timeout);
extern long setTimeout(std::function<void(void)> *handler, long timeout);
extern long setTimeout(std::function<void(void)> handler, long timeout);
extern void stop();

NAMESPACE_HTML5_END;
2 changes: 2 additions & 0 deletions include/libhtml5.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
NAMESPACE_HTML5_BEGIN;

extern Window *window;
extern Console *console;
extern Document *document;

NAMESPACE_HTML5_END;
7 changes: 7 additions & 0 deletions src/export.cc
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,6 +17,10 @@ class __runtime__ {

window = Window::create();
window->retain();
console = window->console;
console->retain();
document = window->document;
document->retain();
g_initialized = true;
}
};
Expand Down

0 comments on commit 25420eb

Please sign in to comment.