Skip to content

Commit

Permalink
Support Node 12
Browse files Browse the repository at this point in the history
  • Loading branch information
agata committed Jan 9, 2020
1 parent 33b102e commit 784f108
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "build/Release/fontmanager",
"types": "index.d.ts",
"dependencies": {
"nan": ">=2.10.0"
"nan": ">=2.14.0"
},
"devDependencies": {
"mocha": "*"
Expand Down
60 changes: 37 additions & 23 deletions src/FontDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,26 @@ struct FontDescriptor {
Local<Object> toJSObject() {
Nan::EscapableHandleScope scope;
Local<Object> res = Nan::New<Object>();

if (path) {
Nan::Set(res, Nan::New<String>("path").ToLocalChecked(), Nan::New<String>(path).ToLocalChecked());
Nan::Set(res, Nan::New("path").ToLocalChecked(), Nan::New<String>(path).ToLocalChecked());
}

if (postscriptName) {
Nan::Set(res, Nan::New<String>("postscriptName").ToLocalChecked(), Nan::New<String>(postscriptName).ToLocalChecked());
Nan::Set(res, Nan::New("postscriptName").ToLocalChecked(), Nan::New<String>(postscriptName).ToLocalChecked());
}

if (family) {
Nan::Set(res, Nan::New<String>("family").ToLocalChecked(), Nan::New<String>(family).ToLocalChecked());
Nan::Set(res, Nan::New("family").ToLocalChecked(), Nan::New<String>(family).ToLocalChecked());
}

if (style) {
Nan::Set(res, Nan::New<String>("style").ToLocalChecked(), Nan::New<String>(style).ToLocalChecked());
Nan::Set(res, Nan::New("style").ToLocalChecked(), Nan::New<String>(style).ToLocalChecked());
}
Nan::Set(res, Nan::New<String>("weight").ToLocalChecked(), Nan::New<Number>(weight));
Nan::Set(res, Nan::New<String>("width").ToLocalChecked(), Nan::New<Number>(width));
Nan::Set(res, Nan::New<String>("italic").ToLocalChecked(), Nan::New<v8::Boolean>(italic));
Nan::Set(res, Nan::New<String>("monospace").ToLocalChecked(), Nan::New<v8::Boolean>(monospace));

Nan::Set(res, Nan::New("weight").ToLocalChecked(), Nan::New<Number>(weight));
Nan::Set(res, Nan::New("width").ToLocalChecked(), Nan::New<Number>(width));
Nan::Set(res, Nan::New("italic").ToLocalChecked(), Nan::New<v8::Boolean>(italic));
Nan::Set(res, Nan::New("monospace").ToLocalChecked(), Nan::New<v8::Boolean>(monospace));
return scope.Escape(res);
}

Expand All @@ -148,32 +147,47 @@ struct FontDescriptor {

char *getString(Local<Object> obj, const char *name) {
Nan::HandleScope scope;
MaybeLocal<Value> value = Nan::Get(obj, Nan::New<String>(name).ToLocalChecked());
Nan::MaybeLocal<v8::Value> maybeValue = Nan::Get(obj, Nan::New(name).ToLocalChecked());

if (maybeValue.IsEmpty()) {
return NULL;
}

if (!value.IsEmpty() && value.ToLocalChecked()->IsString()) {
return copyString(*Nan::Utf8String(value.ToLocalChecked()));
v8::Local<v8::Value> value = maybeValue.ToLocalChecked();
if (value->IsString()) {
return copyString(*Nan::Utf8String(Nan::To<v8::String>(value).ToLocalChecked()));
}

return NULL;
}

int getNumber(Local<Object> obj, const char *name) {
Nan::HandleScope scope;
MaybeLocal<Value> value = Nan::Get(obj, Nan::New<String>(name).ToLocalChecked());
Nan::MaybeLocal<v8::Value> maybeValue = Nan::Get(obj, Nan::New(name).ToLocalChecked());

if (maybeValue.IsEmpty()) {
return 0;
}

if (!value.IsEmpty() && value.ToLocalChecked()->IsNumber()) {
return value.ToLocalChecked()->Int32Value(Nan::GetCurrentContext()).FromJust();
v8::Local<v8::Value> value = maybeValue.ToLocalChecked();
if (value->IsNumber()) {
return Nan::To<int>(value).FromJust();
}

return 0;
}

bool getBool(Local<Object> obj, const char *name) {
Nan::HandleScope scope;
MaybeLocal<Value> value = Nan::Get(obj, Nan::New<String>(name).ToLocalChecked());
Nan::MaybeLocal<v8::Value> maybeValue = Nan::Get(obj, Nan::New(name).ToLocalChecked());

if (maybeValue.IsEmpty()) {
return false;
}

if (!value.IsEmpty() && value.ToLocalChecked()->IsBoolean()) {
return value.ToLocalChecked()->BooleanValue(Nan::GetCurrentContext()).FromJust();
v8::Local<v8::Value> value = maybeValue.ToLocalChecked();
if (value->IsBoolean()) {
return Nan::To<bool>(value).FromJust();
}

return false;
Expand All @@ -189,4 +203,4 @@ class ResultSet : public std::vector<FontDescriptor *> {
}
};

#endif
#endif

0 comments on commit 784f108

Please sign in to comment.