Skip to content

Commit

Permalink
add ScriptOrigin
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Aug 4, 2021
1 parent 24a211e commit d09debf
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Makefile
Expand Up @@ -32,6 +32,7 @@ LINT_SOURCES = \
nan_persistent_12_inl.h \
nan_persistent_pre_12_inl.h \
nan_private.h \
nan_scriptorigin.h \
nan_string_bytes.h \
nan_weak.h \
test/cpp/accessors.cpp \
Expand Down Expand Up @@ -111,5 +112,6 @@ $(ADDONS): nan.h nan_new.h nan_implementation_pre_12_inl.h nan_implementation_12
nan_define_own_property_helper.h \
nan_json.h nan_maybe_43_inl.h nan_maybe_pre_43_inl.h \
nan_persistent_12_inl.h nan_persistent_pre_12_inl.h nan_private.h \
nan_weak.h nan_string_bytes.h test/binding.gyp $(SOURCES)
nan_weak.h nan_scriptorigin.h nan_string_bytes.h \
test/binding.gyp $(SOURCES)
cd test/ && ../node_modules/.bin/node-gyp rebuild
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -205,10 +205,11 @@ The `Nan::MaybeLocal` and `Nan::Maybe` types are monads that encapsulate `v8::Lo

### Script

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

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


### JSON
Expand Down
24 changes: 22 additions & 2 deletions doc/script.md
@@ -1,9 +1,10 @@
## Script

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

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


<a name="api_nan_compile_script"></a>
Expand Down Expand Up @@ -34,5 +35,24 @@ Signature:
```c++
Nan::MaybeLocal<v8::Value> Nan::RunScript(v8::Local<Nan::UnboundScript> script)
Nan::MaybeLocal<v8::Value> Nan::RunScript(v8::Local<Nan::BoundScript> script)
Nan::MaybeLocal<v8::Value> Nan::RunScript(v8::Local<Nan::BoundScript> script)
```

<a name="api_nan_script_origin"></a>
### Nan::ScriptOrigin

A class transparently extending [`v8::ScriptOrigin`](https://v8docs.nodesource.com/node-16.0/db/d84/classv8_1_1_script_origin.html#pub-methods)
to provide backwards compatibility. Only the listed methods are guaranteed to
be available on all versions of Node.

Declaration:

```c++
class Nan::ScriptOrigin : public v8::ScriptOrigin {
public:
ScriptOrigin(v8::Local<v8::Value> name, v8::Local<v8::Integer> line = v8::Local<v8::Integer>(), v8::Local<v8::Integer> column = v8::Local<v8::Integer>())
v8::Local<v8::Value> ResourceName() const;
v8::Local<v8::Integer> ResourceLineOffset() const;
v8::Local<v8::Integer> ResourceColumnOffset() const;
}
```
6 changes: 6 additions & 0 deletions nan.h
Expand Up @@ -41,6 +41,8 @@
#define NODE_12_0_MODULE_VERSION 72
#define NODE_13_0_MODULE_VERSION 79
#define NODE_14_0_MODULE_VERSION 83
#define NODE_15_0_MODULE_VERSION 88
#define NODE_16_0_MODULE_VERSION 93

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

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

//=== ScriptOrigin =============================================================

#include "nan_scriptorigin.h" // NOLINT(build/include)

} // end of namespace Nan

#endif // NAN_H_
76 changes: 76 additions & 0 deletions nan_scriptorigin.h
@@ -0,0 +1,76 @@
/*********************************************************************
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2021 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/

#ifndef NAN_SCRIPTORIGIN_H_
#define NAN_SCRIPTORIGIN_H_

class ScriptOrigin : public v8::ScriptOrigin {
public:
#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 9 || \
(V8_MAJOR_VERSION == 9 && (defined(V8_MINOR_VERSION) && (V8_MINOR_VERSION > 0\
|| (V8_MINOR_VERSION == 0 && defined(V8_BUILD_NUMBER) \
&& V8_BUILD_NUMBER >= 1)))))
explicit ScriptOrigin(v8::Local<v8::Value> name) :
v8::ScriptOrigin(v8::Isolate::GetCurrent(), name) {}

ScriptOrigin(v8::Local<v8::Value> name
, v8::Local<v8::Integer> line) :
v8::ScriptOrigin(v8::Isolate::GetCurrent()
, name
, To<int32_t>(line).FromMaybe(0)) {}

ScriptOrigin(v8::Local<v8::Value> name
, v8::Local<v8::Integer> line
, v8::Local<v8::Integer> column) :
v8::ScriptOrigin(v8::Isolate::GetCurrent()
, name
, To<int32_t>(line).FromMaybe(0)
, To<int32_t>(column).FromMaybe(0)) {}
#elif defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 8 || \
(V8_MAJOR_VERSION == 8 && (defined(V8_MINOR_VERSION) && (V8_MINOR_VERSION > 9\
|| (V8_MINOR_VERSION == 9 && defined(V8_BUILD_NUMBER) \
&& V8_BUILD_NUMBER >= 45)))))
explicit ScriptOrigin(v8::Local<v8::Value> name) : v8::ScriptOrigin(name) {}

ScriptOrigin(v8::Local<v8::Value> name
, v8::Local<v8::Integer> line) :
v8::ScriptOrigin(name, To<int32_t>(line).FromMaybe(0)) {}

ScriptOrigin(v8::Local<v8::Value> name
, v8::Local<v8::Integer> line
, v8::Local<v8::Integer> column) :
v8::ScriptOrigin(name
, To<int32_t>(line).FromMaybe(0)
, To<int32_t>(column).FromMaybe(0)) {}
#else
explicit ScriptOrigin(v8::Local<v8::Value> name) : v8::ScriptOrigin(name) {}

ScriptOrigin(v8::Local<v8::Value> name
, v8::Local<v8::Integer> line) : v8::ScriptOrigin(name, line) {}

ScriptOrigin(v8::Local<v8::Value> name
, v8::Local<v8::Integer> line
, v8::Local<v8::Integer> column) :
v8::ScriptOrigin(name, line, column) {}
#endif

#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 8 || \
(V8_MAJOR_VERSION == 8 && (defined(V8_MINOR_VERSION) && (V8_MINOR_VERSION > 9\
|| (V8_MINOR_VERSION == 9 && defined(V8_BUILD_NUMBER) \
&& V8_BUILD_NUMBER >= 45)))))
v8::Local<v8::Integer> ResourceLineOffset() const {
return New(LineOffset());
}

v8::Local<v8::Integer> ResourceColumnOffset() const {
return New(ColumnOffset());
}
#endif
};

#endif // NAN_SCRIPTORIGIN_H_
7 changes: 1 addition & 6 deletions test/cpp/nannew.cpp
Expand Up @@ -246,12 +246,7 @@ NAN_METHOD(testScript) {

t.plan(6);

#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 8 || \
(V8_MAJOR_VERSION == 8 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 9))
ScriptOrigin origin(New("foo").ToLocalChecked(), 5);
#else
ScriptOrigin origin(New("foo").ToLocalChecked(), New(5));
#endif
Nan::ScriptOrigin origin(New("foo").ToLocalChecked(), New(5));

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

0 comments on commit d09debf

Please sign in to comment.