Skip to content

Commit d09debf

Browse files
committed
add ScriptOrigin
1 parent 24a211e commit d09debf

File tree

6 files changed

+110
-10
lines changed

6 files changed

+110
-10
lines changed

Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ LINT_SOURCES = \
3232
nan_persistent_12_inl.h \
3333
nan_persistent_pre_12_inl.h \
3434
nan_private.h \
35+
nan_scriptorigin.h \
3536
nan_string_bytes.h \
3637
nan_weak.h \
3738
test/cpp/accessors.cpp \
@@ -111,5 +112,6 @@ $(ADDONS): nan.h nan_new.h nan_implementation_pre_12_inl.h nan_implementation_12
111112
nan_define_own_property_helper.h \
112113
nan_json.h nan_maybe_43_inl.h nan_maybe_pre_43_inl.h \
113114
nan_persistent_12_inl.h nan_persistent_pre_12_inl.h nan_private.h \
114-
nan_weak.h nan_string_bytes.h test/binding.gyp $(SOURCES)
115+
nan_weak.h nan_scriptorigin.h nan_string_bytes.h \
116+
test/binding.gyp $(SOURCES)
115117
cd test/ && ../node_modules/.bin/node-gyp rebuild

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,11 @@ The `Nan::MaybeLocal` and `Nan::Maybe` types are monads that encapsulate `v8::Lo
205205

206206
### Script
207207

208-
NAN provides a `v8::Script` helpers as the API has changed over the supported versions of V8.
208+
NAN provides `v8::Script` helpers as the API has changed over the supported versions of V8.
209209

210210
- <a href="doc/script.md#api_nan_compile_script"><b><code>Nan::CompileScript()</code></b></a>
211211
- <a href="doc/script.md#api_nan_run_script"><b><code>Nan::RunScript()</code></b></a>
212+
- <a href="doc/script.md#api_nan_script_origin"><b><code>Nan::ScriptOrigin</code></b></a>
212213

213214

214215
### JSON

doc/script.md

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
## Script
22

3-
NAN provides a `v8::Script` helpers as the API has changed over the supported versions of V8.
3+
NAN provides `v8::Script` helpers as the API has changed over the supported versions of V8.
44

55
- <a href="#api_nan_compile_script"><b><code>Nan::CompileScript()</code></b></a>
66
- <a href="#api_nan_run_script"><b><code>Nan::RunScript()</code></b></a>
7+
- <a href="#api_nan_script_origin"><b><code>Nan::ScriptOrigin</code></b></a>
78

89

910
<a name="api_nan_compile_script"></a>
@@ -34,5 +35,24 @@ Signature:
3435
3536
```c++
3637
Nan::MaybeLocal<v8::Value> Nan::RunScript(v8::Local<Nan::UnboundScript> script)
37-
Nan::MaybeLocal<v8::Value> Nan::RunScript(v8::Local<Nan::BoundScript> script)
38+
Nan::MaybeLocal<v8::Value> Nan::RunScript(v8::Local<Nan::BoundScript> script)
39+
```
40+
41+
<a name="api_nan_script_origin"></a>
42+
### Nan::ScriptOrigin
43+
44+
A class transparently extending [`v8::ScriptOrigin`](https://v8docs.nodesource.com/node-16.0/db/d84/classv8_1_1_script_origin.html#pub-methods)
45+
to provide backwards compatibility. Only the listed methods are guaranteed to
46+
be available on all versions of Node.
47+
48+
Declaration:
49+
50+
```c++
51+
class Nan::ScriptOrigin : public v8::ScriptOrigin {
52+
public:
53+
ScriptOrigin(v8::Local<v8::Value> name, v8::Local<v8::Integer> line = v8::Local<v8::Integer>(), v8::Local<v8::Integer> column = v8::Local<v8::Integer>())
54+
v8::Local<v8::Value> ResourceName() const;
55+
v8::Local<v8::Integer> ResourceLineOffset() const;
56+
v8::Local<v8::Integer> ResourceColumnOffset() const;
57+
}
3858
```

nan.h

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#define NODE_12_0_MODULE_VERSION 72
4242
#define NODE_13_0_MODULE_VERSION 79
4343
#define NODE_14_0_MODULE_VERSION 83
44+
#define NODE_15_0_MODULE_VERSION 88
45+
#define NODE_16_0_MODULE_VERSION 93
4446

4547
#ifdef _MSC_VER
4648
# define NAN_HAS_CPLUSPLUS_11 (_MSC_VER >= 1800)
@@ -2893,6 +2895,10 @@ MakeMaybe(MaybeMaybe<T> v) {
28932895

28942896
#include "nan_json.h" // NOLINT(build/include)
28952897

2898+
//=== ScriptOrigin =============================================================
2899+
2900+
#include "nan_scriptorigin.h" // NOLINT(build/include)
2901+
28962902
} // end of namespace Nan
28972903

28982904
#endif // NAN_H_

nan_scriptorigin.h

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*********************************************************************
2+
* NAN - Native Abstractions for Node.js
3+
*
4+
* Copyright (c) 2021 NAN contributors
5+
*
6+
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7+
********************************************************************/
8+
9+
#ifndef NAN_SCRIPTORIGIN_H_
10+
#define NAN_SCRIPTORIGIN_H_
11+
12+
class ScriptOrigin : public v8::ScriptOrigin {
13+
public:
14+
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 9 || \
15+
(V8_MAJOR_VERSION == 9 && (defined(V8_MINOR_VERSION) && (V8_MINOR_VERSION > 0\
16+
|| (V8_MINOR_VERSION == 0 && defined(V8_BUILD_NUMBER) \
17+
&& V8_BUILD_NUMBER >= 1)))))
18+
explicit ScriptOrigin(v8::Local<v8::Value> name) :
19+
v8::ScriptOrigin(v8::Isolate::GetCurrent(), name) {}
20+
21+
ScriptOrigin(v8::Local<v8::Value> name
22+
, v8::Local<v8::Integer> line) :
23+
v8::ScriptOrigin(v8::Isolate::GetCurrent()
24+
, name
25+
, To<int32_t>(line).FromMaybe(0)) {}
26+
27+
ScriptOrigin(v8::Local<v8::Value> name
28+
, v8::Local<v8::Integer> line
29+
, v8::Local<v8::Integer> column) :
30+
v8::ScriptOrigin(v8::Isolate::GetCurrent()
31+
, name
32+
, To<int32_t>(line).FromMaybe(0)
33+
, To<int32_t>(column).FromMaybe(0)) {}
34+
#elif defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 8 || \
35+
(V8_MAJOR_VERSION == 8 && (defined(V8_MINOR_VERSION) && (V8_MINOR_VERSION > 9\
36+
|| (V8_MINOR_VERSION == 9 && defined(V8_BUILD_NUMBER) \
37+
&& V8_BUILD_NUMBER >= 45)))))
38+
explicit ScriptOrigin(v8::Local<v8::Value> name) : v8::ScriptOrigin(name) {}
39+
40+
ScriptOrigin(v8::Local<v8::Value> name
41+
, v8::Local<v8::Integer> line) :
42+
v8::ScriptOrigin(name, To<int32_t>(line).FromMaybe(0)) {}
43+
44+
ScriptOrigin(v8::Local<v8::Value> name
45+
, v8::Local<v8::Integer> line
46+
, v8::Local<v8::Integer> column) :
47+
v8::ScriptOrigin(name
48+
, To<int32_t>(line).FromMaybe(0)
49+
, To<int32_t>(column).FromMaybe(0)) {}
50+
#else
51+
explicit ScriptOrigin(v8::Local<v8::Value> name) : v8::ScriptOrigin(name) {}
52+
53+
ScriptOrigin(v8::Local<v8::Value> name
54+
, v8::Local<v8::Integer> line) : v8::ScriptOrigin(name, line) {}
55+
56+
ScriptOrigin(v8::Local<v8::Value> name
57+
, v8::Local<v8::Integer> line
58+
, v8::Local<v8::Integer> column) :
59+
v8::ScriptOrigin(name, line, column) {}
60+
#endif
61+
62+
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 8 || \
63+
(V8_MAJOR_VERSION == 8 && (defined(V8_MINOR_VERSION) && (V8_MINOR_VERSION > 9\
64+
|| (V8_MINOR_VERSION == 9 && defined(V8_BUILD_NUMBER) \
65+
&& V8_BUILD_NUMBER >= 45)))))
66+
v8::Local<v8::Integer> ResourceLineOffset() const {
67+
return New(LineOffset());
68+
}
69+
70+
v8::Local<v8::Integer> ResourceColumnOffset() const {
71+
return New(ColumnOffset());
72+
}
73+
#endif
74+
};
75+
76+
#endif // NAN_SCRIPTORIGIN_H_

test/cpp/nannew.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,7 @@ NAN_METHOD(testScript) {
246246

247247
t.plan(6);
248248

249-
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 8 || \
250-
(V8_MAJOR_VERSION == 8 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 9))
251-
ScriptOrigin origin(New("foo").ToLocalChecked(), 5);
252-
#else
253-
ScriptOrigin origin(New("foo").ToLocalChecked(), New(5));
254-
#endif
249+
Nan::ScriptOrigin origin(New("foo").ToLocalChecked(), New(5));
255250

256251
t.ok(_( assertType<Script>(New<Script>(
257252
New("2 + 3").ToLocalChecked()).ToLocalChecked())));

0 commit comments

Comments
 (0)