Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[node] Bus Error 10
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemorris committed Jul 2, 2016
1 parent 37d8651 commit 605ff8f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions platform/node/src/node_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,10 @@ NodeMap::~NodeMap() {
std::unique_ptr<mbgl::AsyncRequest> NodeMap::request(const mbgl::Resource& resource, Callback callback_) {
Nan::HandleScope scope;

auto worker = new NodeRequestWorker(handle()->GetInternalField(1)->ToObject(), resource, callback_);
worker->Execute();
NodeRequestWorker worker(handle()->GetInternalField(1)->ToObject(), resource, callback_);
worker.Execute();

return std::make_unique<NodeRequestWorker::NodeAsyncRequest>(worker);
return std::make_unique<NodeRequestWorker::NodeAsyncRequest>(&worker);
}

} // namespace node_mbgl
38 changes: 27 additions & 11 deletions platform/node/src/node_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <mbgl/util/chrono.hpp>

#include <cmath>
#include <iostream>

namespace node_mbgl {

Expand All @@ -13,9 +14,20 @@ NodeRequestWorker::NodeRequestWorker(
: AsyncWorker(nullptr),
nodeMapHandle(nodeMapHandle_),
resource(resource_),
fileSourceCallback(std::make_unique<mbgl::FileSource::Callback>(fileSourceCallback_)) {}
fileSourceCallback(std::make_unique<mbgl::FileSource::Callback>(fileSourceCallback_)) {
Nan::HandleScope scope;

auto fn = Nan::New(handleCallback);

// Bind a reference to this object on the callback function
fn->SetHiddenValue(Nan::New("worker").ToLocalChecked(), Nan::New<v8::External>(this));

NodeRequestWorker::~NodeRequestWorker() {}
callback.Reset(fn);
}

NodeRequestWorker::~NodeRequestWorker() {
std::cout << "~NodeRequestWorker" << std::endl;
}

void NodeRequestWorker::Execute() {
Nan::HandleScope scope;
Expand All @@ -25,20 +37,17 @@ void NodeRequestWorker::Execute() {
Nan::Set(req, Nan::New("url").ToLocalChecked(), Nan::New<v8::String>(resource.url).ToLocalChecked());
Nan::Set(req, Nan::New("kind").ToLocalChecked(), Nan::New<v8::Integer>(resource.kind));

auto callback = Nan::New(handleCallback);

// Bind a reference to this object on the callback function
callback->SetHiddenValue(Nan::New("worker").ToLocalChecked(), Nan::New<v8::External>(this));

v8::Local<v8::Value> argv[] = {
req,
callback
Nan::New(callback)
};

Nan::MakeCallback(nodeMapHandle, "request", 2, argv);
}

void NodeRequestWorker::Destroy() {
std::cout << "Destroy" << !fileSourceCallback << std::endl;

// When this object gets destroyed, make sure that the
// AsyncRequest can no longer attempt to remove the callback function
// this object was holding (it can't be fired anymore).
Expand All @@ -48,8 +57,10 @@ void NodeRequestWorker::Destroy() {
}

void NodeRequestWorker::WorkComplete() {
std::cout << "WorkComplete" << !fileSourceCallback << std::endl;

// If callback has already been called, no-op
if (!fileSourceCallback) return;
if (!fileSourceCallback) return Destroy();

ErrorMessage() ? HandleErrorCallback() : HandleOKCallback();
}
Expand Down Expand Up @@ -144,8 +155,13 @@ void NodeRequestWorker::HandleErrorCallback() {
cb = nullptr;
}

NodeRequestWorker::NodeRequest::NodeRequest() {}
NodeRequestWorker::NodeRequest::~NodeRequest() {}
NodeRequestWorker::NodeRequest::NodeRequest() {
std::cout << "NodeRequest" << std::endl;
}

NodeRequestWorker::NodeRequest::~NodeRequest() {
std::cout << "~NodeRequest" << std::endl;
}

Nan::Persistent<v8::Function> NodeRequestWorker::NodeRequest::constructor;

Expand Down
2 changes: 2 additions & 0 deletions platform/node/src/node_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class NodeRequestWorker : public Nan::AsyncWorker {
const mbgl::Resource& resource;
std::unique_ptr<mbgl::FileSource::Callback> fileSourceCallback;
NodeAsyncRequest* asyncRequest = nullptr;

Nan::Persistent<v8::Function> callback;
mbgl::Response response;

static Nan::Persistent<v8::Function> handleCallback;
Expand Down

0 comments on commit 605ff8f

Please sign in to comment.