Skip to content

Commit 8421f45

Browse files
committed
start napi vor v12
1 parent d13b876 commit 8421f45

File tree

16 files changed

+1051
-1942
lines changed

16 files changed

+1051
-1942
lines changed

.vscode/launch.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "(lldb) Lancer",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"program": "/Users/dieudonn/.nvm/versions/node/v12.13.0/bin/node",
9+
"args": [
10+
"/Users/dieudonn/Dev/node-libxml/test/libxml-test.js"
11+
],
12+
"stopAtEntry": true,
13+
"cwd": "${workspaceFolder}",
14+
"environment": [],
15+
"externalConsole": false,
16+
"MIMode": "lldb"
17+
},
18+
{
19+
"type": "lldb",
20+
"request": "launch",
21+
"name": "Launch Program",
22+
"preLaunchTask": "npm: build:dev",
23+
"program": "/Users/dieudonn/.nvm/versions/node/v12.13.0/bin/node",
24+
"args": [
25+
"/Users/dieudonn/Dev/node-libxml/test/index.js"
26+
]
27+
}
28+
]
29+
}

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files.associations": {
3+
"*.twig": "twig",
4+
"ios": "cpp",
5+
"vector": "cpp"
6+
}
7+
}

binding.gyp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,34 @@
22
'targets': [
33
{
44
'target_name': 'xml',
5+
'cflags!': [ '-fno-exceptions' ],
6+
'cflags_cc!': [ '-fno-exceptions' ],
7+
'xcode_settings': { 'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
8+
'CLANG_CXX_LIBRARY': 'libc++',
9+
'MACOSX_DEPLOYMENT_TARGET': '10.7',
10+
},
11+
'msvs_settings': {
12+
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
13+
},
514
'product_extension': 'node',
615
'type': 'shared_library',
716
'include_dirs': [
17+
'<!@(node -p "require(\'node-addon-api\').include")',
818
'vendor/libxml/include',
9-
"<!(node -e \"require('nan')\")"
19+
"<!@(node -p \"require('node-addon-api').include\")",
1020
],
1121
'cflags': [ '-Wall' ],
22+
'cflags!': [ '-fno-exceptions' ],
23+
'cflags_cc!': [ '-fno-exceptions' ],
1224
'xcode_settings': {
25+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
26+
'CLANG_CXX_LIBRARY': 'libc++',
27+
'MACOSX_DEPLOYMENT_TARGET': '10.7',
1328
'OTHER_CFLAGS': [ '-Wall' ]
1429
},
1530
'sources': [
1631
'libxml.cpp',
17-
'libxml-syntax-error.cpp',
32+
# 'libxml-syntax-error.cpp',
1833
'vendor/libxml/buf.c',
1934
'vendor/libxml/catalog.c',
2035
'vendor/libxml/chvalid.c',
@@ -56,7 +71,9 @@
5671
],
5772
'conditions': [
5873
['OS=="mac"', {
74+
'cflags+': ['-fvisibility=hidden'],
5975
'xcode_settings': {
76+
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
6077
'CLANG_CXX_LANGUAGE_STANDARD': 'c++11',
6178
'OTHER_LDFLAGS': [
6279
'-undefined dynamic_lookup'

libxml-syntax-error.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,58 @@
55
#include <iostream>
66
#include "libxml-syntax-error.h"
77

8-
void setStringField(v8::Local<v8::Object> obj, const char* name, const char* value) {
9-
Nan::HandleScope scope;
10-
if (!value) {
8+
void setStringField(Napi::Object obj, const char *name, const char *value, Napi::Env env)
9+
{
10+
Napi::HandleScope scope(env);
11+
if (!value)
12+
{
1113
return;
1214
}
13-
Nan::Set(obj, Nan::New<v8::String>(name).ToLocalChecked(), Nan::New<v8::String>(value, strlen(value)).ToLocalChecked());
15+
(obj).Set(Napi::String::New(env, name), Napi::String::New(env, value, strlen(value)));
1416
}
1517

16-
void setNumericField(v8::Local<v8::Object> obj, const char* name, const int value) {
17-
Nan::HandleScope scope;
18-
Nan::Set(obj, Nan::New<v8::String>(name).ToLocalChecked(), Nan::New<v8::Int32>(value));
18+
void setNumericField(Napi::Object obj, const char *name, const int value, Napi::Env env)
19+
{
20+
Napi::HandleScope scope(env);
21+
(obj).Set(Napi::String::New(env, name), Napi::Number::New(env, value));
1922
}
2023

21-
v8::Local<v8::Value>
22-
XmlSyntaxError::BuildSyntaxError(xmlError* error) {
23-
Nan::EscapableHandleScope scope;
24+
Napi::Value
25+
XmlSyntaxError::BuildSyntaxError(xmlError *error, Napi::Env env)
26+
{
27+
Napi::EscapableHandleScope scope(env);
2428

25-
v8::Local<v8::Value> err = v8::Exception::Error(
26-
Nan::New<v8::String>(error->message).ToLocalChecked());
27-
v8::Local<v8::Object> out = v8::Local<v8::Object>::Cast(err);
29+
auto err = Napi::TypeError::New(env,
30+
Napi::String::New(env, error->message));
31+
Napi::Object out = Napi::Object();
2832

29-
setStringField(out, "message", error->message);
30-
setNumericField(out, "level", error->level);
31-
setNumericField(out, "column", error->int2);
32-
setStringField(out, "file", error->file);
33-
setNumericField(out, "line", error->line);
33+
setStringField(out, "message", error->message, env);
34+
setNumericField(out, "level", error->level, env);
35+
setNumericField(out, "column", error->int2, env);
36+
setStringField(out, "file", error->file, env);
37+
setNumericField(out, "line", error->line, env);
3438

35-
if (error->int1) {
36-
setNumericField(out, "int1", error->int1);
39+
if (error->int1)
40+
{
41+
setNumericField(out, "int1", error->int1, env);
3742
}
38-
return scope.Escape(err);
43+
return out;
3944
}
4045

41-
int XmlSyntaxError::maxError {100};
46+
int XmlSyntaxError::maxError{100};
4247

43-
void XmlSyntaxError::ChangeMaxNumberOfError(int max){
48+
void XmlSyntaxError::ChangeMaxNumberOfError(int max)
49+
{
4450
XmlSyntaxError::maxError = max;
4551
}
4652

47-
void
48-
XmlSyntaxError::PushToArray(void* errs, xmlError* error) {
49-
Nan::HandleScope scope;
50-
v8::Local<v8::Array> errors = *reinterpret_cast<v8::Local<v8::Array>*>(errs);
51-
if(errors->Length() >= maxError){
53+
void XmlSyntaxError::PushToArray(void *errs, xmlError *error)
54+
{
55+
Napi::Array errors = *reinterpret_cast<Napi::Array *>(errs);
56+
if (errors.Length >= maxError)
57+
{
5258
return;
5359
}
54-
v8::Local<v8::Function> push = v8::Local<v8::Function>::Cast(errors->Get(Nan::New<v8::String>("push").ToLocalChecked()));
55-
v8::Local<v8::Value> argv[1] = { XmlSyntaxError::BuildSyntaxError(error) };
56-
push->Call(errors, 1, argv);
60+
Napi::Value castedError = {XmlSyntaxError::BuildSyntaxError(error, XmlSyntaxError::env)};
61+
errors.Set(errors.Length, castedError);
5762
}

libxml-syntax-error.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55

66
#include "libxml.h"
77

8-
class XmlSyntaxError {
8+
class XmlSyntaxError
9+
{
910
static int maxError;
11+
1012
public:
13+
static Napi::Env env;
1114
static void ChangeMaxNumberOfError(int max);
1215

1316
// push xmlError onto v8::Array
14-
static void PushToArray(void* errs, xmlError* error);
17+
static void PushToArray(void *errs, xmlError *error);
1518

1619
// create a v8 object for the syntax eror
17-
static v8::Local<v8::Value> BuildSyntaxError(xmlError* error);
20+
static Napi::Value BuildSyntaxError(xmlError *error, Napi::Env env);
1821
};
1922

2023
// LIBXML_SYNTAX_ERROR
21-
#endif
24+
#endif

0 commit comments

Comments
 (0)