Skip to content

Commit

Permalink
add fetch test
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Jul 10, 2019
1 parent 2c3f993 commit 71cd61d
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 3 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
22 changes: 22 additions & 0 deletions examples/promise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ static void promiseTest()
html5::window->console->log(promise);
}

static void fetchTest()
{
HTML5_INIT();
std::string url = "http://localhost/test/free.jpg";
html5::fetch(url)->then([](const html5::Object &_response)->html5::Promise* {
auto response = dynamic_cast<html5::Response *>(const_cast<html5::Object *>(&_response));
return response->blob();
})->then([](const html5::Object &_blob)->html5::Promise* {
auto blob = dynamic_cast<html5::Blob *>(const_cast<html5::Object *>(&_blob));
auto reader = html5::FileReader::create();
reader->onload = [reader](html5::Event *event) {
auto data = reader->result;
auto image = html5::HTMLImageElement::create();
image->src = data;
html5::window->document->appendChild(image);
};
reader->readAsDataURL(blob);
return nullptr;
});
}

EMSCRIPTEN_BINDINGS(Promise) {
emscripten::function("promiseTest", &promiseTest);
emscripten::function("fetchTest", &fetchTest);
}
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
2 changes: 1 addition & 1 deletion include/promise.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ class Promise : public Object {
Promise *then(PromiseStringPromiseFunction onFulfilled, PromiseDoublePromiseFunction onRejected);
Promise *then(PromiseStringPromiseFunction onFulfilled, PromiseStringPromiseFunction onRejected);
Promise *then(PromiseStringPromiseFunction onFulfilled, PromiseObjectPromiseFunction onRejected);
Promise *then(PromiseObjectFunction onFulfilled);
Promise *then(PromiseObjectFunction onFulfilled, PromiseVoidFunction onRejected);
Promise *then(PromiseObjectFunction onFulfilled, PromiseDoubleFunction onRejected);
Promise *then(PromiseObjectFunction onFulfilled, PromiseStringFunction onRejected);
Promise *then(PromiseObjectFunction onFulfilled, PromiseObjectFunction onRejected);
Promise *then(PromiseObjectPromiseFunction onFulfilled);
//Promise *then(PromiseObjectFunction onFulfilled);
Promise *then(PromiseObjectPromiseFunction onFulfilled, PromiseVoidFunction onRejected);
Promise *then(PromiseObjectPromiseFunction onFulfilled, PromiseDoubleFunction onRejected);
Promise *then(PromiseObjectPromiseFunction onFulfilled, PromiseStringFunction onRejected);
Expand Down
36 changes: 36 additions & 0 deletions include/response.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "libhtml5.h"

NAMESPACE_HTML5_BEGIN;

class Headers;
class ReadableStream;

class Response : public Object {
public:

HTML5_READONLY_PROPERTY_OBJECT(Response, Headers, headers);
HTML5_READONLY_PROPERTY(Response, bool, ok);
HTML5_READONLY_PROPERTY(Response, bool, redirected);
HTML5_READONLY_PROPERTY(Response, int, status);
HTML5_READONLY_PROPERTY(Response, std::string, type);
HTML5_READONLY_PROPERTY(Response, std::string, url);
HTML5_PROPERTY(Response, bool, useFinalURL);
HTML5_READONLY_PROPERTY_OBJECT(Response, ReadableStream, body);
HTML5_READONLY_PROPERTY(Response, bool, bodyUsed);

Response(emscripten::val v);
virtual ~Response();
static Response *create(emscripten::val v);
Response *clone();
Response *error();
Response *redirect(const std::string &url, int status = 0);
Promise *arrayBuffer();
Promise *blob();
Promise *formData();
Promise *json();
Promise *text();
};

NAMESPACE_HTML5_END;
1 change: 1 addition & 0 deletions include/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class Window : public EventTarget {
Promise *createImageBitmap(ImageData *image, long sx, long sy, long sw, long sh);
Promise *createImageBitmap(CanvasRenderingContext2D *image, long sx, long sy, long sw, long sh);
Promise *createImageBitmap(ImageBitmap *image, long sx, long sy, long sw, long sh);
Promise *fetch(const std::string &url);
void focus();
CSSStyleDeclaration *getComputedStyle(Element *elt, std::string pseudoElt);
WindowProxy *getter(void *indexName);
Expand Down
5 changes: 5 additions & 0 deletions src/export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ Promise *createImageBitmap(ImageBitmap *image, long sx, long sy, long sw, long s
return window->createImageBitmap(image, sx, sy, sw, sh);
}

Promise *fetch(const std::string &url)
{
return window->fetch(url);
}

void focus()
{
window->focus();
Expand Down
80 changes: 80 additions & 0 deletions src/file_reader.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "file_reader.h"
#include "blob.h"
#include "array_buffer.h"

USING_NAMESPACE_HTML5;

HTML5_BIND_CLASS(FileReader);

FileReader::FileReader(emscripten::val v) :
EventTarget(v)
{
}

FileReader::~FileReader()
{
}

FileReader *FileReader::create()
{
auto reader = new FileReader(HTML5_NEW_PRIMITIVE_INSTANCE(FileReader));
reader->autorelease();
return reader;
}

void FileReader::abort()
{
HTML5_CALL(this->v, abort);
}

void FileReader::readAsArrayBuffer(Blob *blob)
{
HTML5_CALL(this->v, readAsArrayBuffer, blob->v);
}

void FileReader::readAsArrayBuffer(File *file)
{
// HTML5_CALL(this->v, readAsArrayBuffer, file->v);
}

void FileReader::readAsBinaryString(Blob *blob)
{
HTML5_CALL(this->v, readAsBinaryString, blob->v);
}

void FileReader::readAsBinaryString(File *file)
{
//HTML5_CALL(this->v, readAsBinaryString, file->v);
}

void FileReader::readAsDataURL(Blob *blob)
{
HTML5_CALL(this->v, readAsDataURL, blob->v);
}

void FileReader::readAsDataURL(File *file)
{
//HTML5_CALL(this->v, readAsDataURL, file->v);
}

void FileReader::readAsText(Blob *blob, const std::string &encoding)
{
HTML5_CALL(this->v, readAsText, blob->v, encoding);
}

void FileReader::readAsText(File *file, const std::string &encoding)
{
//HTML5_CALL(this->v, readAsText, file->v, encoding);
}

ArrayBuffer *FileReader::resultArrayBuffer() const
{
return ArrayBuffer::create(this->v["result"]);
}

std::string FileReader::resultString() const
{
return this->v["result"].as<std::string>();
}

HTML5_EVENT_HANDLER_PROPERTY_IMPL_NEW(FileReader, EventHandler, onload);
4 changes: 2 additions & 2 deletions src/promise.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1086,7 +1086,7 @@ Promise *Promise::then(PromiseStringPromiseFunction onFulfilled, PromiseObjectPr
}, this);
return this;
}

/*
Promise *Promise::then(PromiseObjectFunction onFulfilled)
{
auto fn = new PromiseChainFunction();
Expand All @@ -1098,7 +1098,7 @@ Promise *Promise::then(PromiseObjectFunction onFulfilled)
}, this);
return this;
}

*/
Promise *Promise::then(PromiseObjectFunction onFulfilled, PromiseVoidFunction onRejected)
{
auto fn = new PromiseChainFunction();
Expand Down
70 changes: 70 additions & 0 deletions src/response.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "response.h"
#include "promise.h"

USING_NAMESPACE_HTML5;

Response::Response(emscripten::val v) :
Object(v)
{
}

Response::~Response()
{
}

Response *Response::create(emscripten::val v)
{
auto response = new Response(v);
response->autorelease();
return response;
}

Response *Response::clone()
{
return Response::create(HTML5_CALLv(this->v, clone));
}

Response *Response::error()
{
return Response::create(HTML5_CALLv(this->v, error));
}

Response *Response::redirect(const std::string &url, int status)
{
return Response::create(HTML5_CALLv(this->v, redirect, url, status));
}

Promise *Response::arrayBuffer()
{
return Promise::create(HTML5_CALLv(this->v, arrayBuffer));
}

Promise *Response::blob()
{
return Promise::create(HTML5_CALLv(this->v, blob));
}

Promise *Response::formData()
{
return Promise::create(HTML5_CALLv(this->v, formData));
}

Promise *Response::json()
{
return Promise::create(HTML5_CALLv(this->v, json));
}

Promise *Response::text()
{
return Promise::create(HTML5_CALLv(this->v, text));
}

//HTML5_READONLY_PROPERTY_OBJECT_IMPL(Response, Headers, headers);
HTML5_READONLY_PROPERTY_IMPL(Response, bool, ok);
HTML5_READONLY_PROPERTY_IMPL(Response, bool, redirected);
HTML5_READONLY_PROPERTY_IMPL(Response, int, status);
HTML5_READONLY_PROPERTY_IMPL(Response, std::string, type);
HTML5_READONLY_PROPERTY_IMPL(Response, std::string, url);
HTML5_PROPERTY_IMPL(Response, bool, useFinalURL);
//HTML5_READONLY_PROPERTY_OBJECT_IMPL(Response, ReadableStream, body);
HTML5_READONLY_PROPERTY_IMPL(Response, bool, bodyUsed);
5 changes: 5 additions & 0 deletions src/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ Promise *Window::createImageBitmap(ImageBitmap *image, long sx, long sy, long sw
return Promise::create(HTML5_CALLv(this->v, createImageBitmap, image->v, sx, sy, sw, sh));
}

Promise *Window::fetch(const std::string &url)
{
return Promise::create(HTML5_CALLv(this->v, fetch, url));
}

void Window::focus()
{
HTML5_CALL(this->v, focus);
Expand Down

0 comments on commit 71cd61d

Please sign in to comment.