Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Polyfills/Blob/Include/Babylon/Polyfills/Blob.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#pragma once

#include <napi/env.h>
#include <napi/napi.h>
#include <Babylon/Api.h>
#include <vector>
#include <string>

namespace Babylon::Polyfills::Blob
{
void BABYLON_API Initialize(Napi::Env env);

Napi::Value BABYLON_API CreateInstance(
Napi::Env env,
std::vector<std::byte> data,
std::string type);
}
30 changes: 28 additions & 2 deletions Polyfills/Blob/Source/Blob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Babylon::Polyfills::Internal
{
static constexpr auto JS_BLOB_CONSTRUCTOR_NAME = "Blob";

void Blob::Initialize(Napi::Env env)
{
static constexpr auto JS_BLOB_CONSTRUCTOR_NAME = "Blob";
if (env.Global().Get(JS_BLOB_CONSTRUCTOR_NAME).IsUndefined())
{
Napi::Function func = DefineClass(
Expand All @@ -17,13 +18,30 @@ namespace Babylon::Polyfills::Internal
InstanceAccessor("type", &Blob::GetType, nullptr),
InstanceMethod("text", &Blob::Text),
InstanceMethod("arrayBuffer", &Blob::ArrayBuffer),
InstanceMethod("bytes", &Blob::Bytes)
InstanceMethod("bytes", &Blob::Bytes),
});

env.Global().Set(JS_BLOB_CONSTRUCTOR_NAME, func);
}
}

Napi::Value Blob::CreateInstance(
Napi::Env env,
std::vector<std::byte> data,
std::string type)
{
Initialize(env);

auto ctor{env.Global().Get(JS_BLOB_CONSTRUCTOR_NAME).As<Napi::Function>()};
auto jsBlob{ctor.New({})};

auto blob{Blob::Unwrap(jsBlob)};
blob->m_data = std::move(data);
blob->m_type = std::move(type);

return jsBlob;
}

Blob::Blob(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<Blob>(info)
{
Expand Down Expand Up @@ -133,4 +151,12 @@ namespace Babylon::Polyfills::Blob
{
Internal::Blob::Initialize(env);
}

Napi::Value BABYLON_API CreateInstance(
Napi::Env env,
std::vector<std::byte> data,
std::string type)
{
return Internal::Blob::CreateInstance(env, std::move(data), std::move(type));
}
}
5 changes: 5 additions & 0 deletions Polyfills/Blob/Source/Blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ namespace Babylon::Polyfills::Internal
public:
static void Initialize(Napi::Env env);

static Napi::Value CreateInstance(
Napi::Env env,
std::vector<std::byte> data,
std::string type);

explicit Blob(const Napi::CallbackInfo& info);

private:
Expand Down