Skip to content

Commit

Permalink
Merge pull request #28 from knocknote/feature/fully-support-promise
Browse files Browse the repository at this point in the history
Fully support Promise
  • Loading branch information
goccy authored Jul 11, 2019
2 parents 93e45f2 + 1e3273b commit 4e29f2d
Show file tree
Hide file tree
Showing 14 changed files with 1,427 additions and 39 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(SOURCES src/application_cache.cc
src/event_target.cc
src/export.cc
src/external.cc
src/file_reader.cc
src/float32_array.cc
src/float64_array.cc
src/history.cc
Expand Down Expand Up @@ -93,6 +94,7 @@ set(SOURCES src/application_cache.cc
src/range.cc
src/regexp.cc
src/rendering_context.cc
src/response.cc
src/storage.cc
src/string.cc
src/style_sheet.cc
Expand Down
29 changes: 29 additions & 0 deletions examples/promise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,39 @@ static void promiseTest()
});
promise->then([](const std::string &value) {
html5::window->console->log(value);
return html5::Promise::resolve("bar");
})->then([](const std::string &value) {
html5::window->console->log(value);
return html5::Promise::resolve(0.0);
});
html5::window->console->log(promise);
}

static void fetchImageTest(const std::string &imageURL)
{
HTML5_INIT();
html5::fetch(imageURL)->then<html5::Response *>([](html5::Response *response) {
return response->blob();
})->then<html5::Blob *>([](html5::Blob *blob) {
auto reader = html5::FileReader::create();
reader->onload = [reader](html5::Event *event) {
std::string data = reader->result;
auto image = html5::HTMLImageElement::create();
image->src = data;
html5::window->document->body->appendChild(image);
};
reader->readAsDataURL(blob);
return nullptr;
})->catchError([]{
html5::window->console->log("catch");
return nullptr;
})->finally([]{
html5::window->console->log("finally");
return nullptr;
});
}

EMSCRIPTEN_BINDINGS(Promise) {
emscripten::function("promiseTest", &promiseTest);
emscripten::function("fetchImageTest", &fetchImageTest);
}
2 changes: 2 additions & 0 deletions include/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "event.h"
#include "event_target.h"
#include "external.h"
#include "file_reader.h"
#include "float32_array.h"
#include "float64_array.h"
#include "history.h"
Expand Down Expand Up @@ -84,6 +85,7 @@
#include "processing_instruction.h"
#include "promise.h"
#include "range.h"
#include "response.h"
#include "rendering_context.h"
#include "storage.h"
#include "style_sheet.h"
Expand Down
1 change: 1 addition & 0 deletions include/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ extern Promise *createImageBitmap(Blob *image, long sx, long sy, long sw, long s
extern Promise *createImageBitmap(ImageData *image, long sx, long sy, long sw, long sh);
extern Promise *createImageBitmap(CanvasRenderingContext2D *image, long sx, long sy, long sw, long sh);
extern Promise *createImageBitmap(ImageBitmap *image, long sx, long sy, long sw, long sh);
extern Promise *fetch(const std::string &url);
extern void focus();
extern CSSStyleDeclaration *getComputedStyle(Element *elt, std::string pseudoElt);
extern WindowProxy *getter(void *indexName);
Expand Down
37 changes: 37 additions & 0 deletions include/file_reader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "event_target.h"

NAMESPACE_HTML5_BEGIN;

class Blob;
class File;
class ArrayBuffer;

class FileReader : public EventTarget {
public:
struct {
FileReader &self;
operator ArrayBuffer *() { return self.resultArrayBuffer(); };
operator std::string () { return self.resultString(); };
} result{*this};
HTML5_EVENT_HANDLER_PROPERTY(FileReader, EventHandler, onload);

FileReader(emscripten::val v);
virtual ~FileReader();
static FileReader *create();
void abort();
void readAsArrayBuffer(Blob *blob);
void readAsArrayBuffer(File *file);
void readAsBinaryString(Blob *blob);
void readAsBinaryString(File *file);
void readAsDataURL(Blob *blob);
void readAsDataURL(File *file);
void readAsText(Blob *blob, const std::string &encoding = "utf-8");
void readAsText(File *file, const std::string &encoding = "utf-8");

ArrayBuffer *resultArrayBuffer() const;
std::string resultString() const;
};

NAMESPACE_HTML5_END;
27 changes: 27 additions & 0 deletions include/libhtml5.h
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,33 @@ template<typename T> std::vector<T *> toObjectArray(emscripten::val v)
} \
HTML5_BIND_METHOD(klass, callback_ ## name);

#define HTML5_EVENT_HANDLER_PROPERTY_IMPL_NEW(klass, type, name) \
type klass::get_ ## name() const \
{ \
HTML5_PROPERTY_TRACE_GETTER(name); \
return this->_ ## name; \
} \
\
void klass::set_ ## name(type value) \
{ \
HTML5_PROPERTY_TRACE_SETTER(name); \
this->_ ## name = value; \
const char *key = __to_text__(to ## klass); \
const char *callbackFnName = __to_text__(callback_ ## name); \
EM_ASM_({ \
var key = Module['toString']($1); \
var callbackFnName = Module['toString']($2); \
var elem = Module[key]($0); \
elem['_value'][#name] = function(e) { elem[callbackFnName](e); }; \
}, this, key, callbackFnName); \
} \
void klass::callback_ ## name(emscripten::val e) \
{ \
if (!this->_ ## name) return; \
(this->_ ## name)(Event::create(e)); \
} \
HTML5_BIND_METHOD(klass, callback_ ## name);

#define HTML5_ERROR_HANDLER_PROPERTY_IMPL(klass, type, name) \
type klass::get_ ## name() const \
{ \
Expand Down
Loading

0 comments on commit 4e29f2d

Please sign in to comment.