This repository has been archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 339
test: add a new.target test to addons-napi #452
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <node_api.h> | ||
#include "../common.h" | ||
|
||
napi_value BaseClass(napi_env env, napi_callback_info info) { | ||
napi_value newTargetArg; | ||
NAPI_CALL(env, napi_get_new_target(env, info, &newTargetArg)); | ||
napi_value thisArg; | ||
NAPI_CALL(env, napi_get_cb_info(env, info, NULL, NULL, &thisArg, NULL)); | ||
napi_value undefined; | ||
NAPI_CALL(env, napi_get_undefined(env, &undefined)); | ||
|
||
// this !== new.target since we are being invoked through super() | ||
bool result; | ||
NAPI_CALL(env, napi_strict_equals(env, newTargetArg, thisArg, &result)); | ||
NAPI_ASSERT(env, !result, "this !== new.target"); | ||
|
||
// new.target !== undefined because we should be called as a new expression | ||
NAPI_CALL(env, napi_strict_equals(env, newTargetArg, undefined, &result)); | ||
NAPI_ASSERT(env, !result, "new.target !== undefined"); | ||
|
||
return thisArg; | ||
} | ||
|
||
napi_value Constructor(napi_env env, napi_callback_info info) { | ||
bool result; | ||
napi_value newTargetArg; | ||
NAPI_CALL(env, napi_get_new_target(env, info, &newTargetArg)); | ||
size_t argc = 1; | ||
napi_value argv; | ||
napi_value thisArg; | ||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &argv, &thisArg, NULL)); | ||
napi_value undefined; | ||
NAPI_CALL(env, napi_get_undefined(env, &undefined)); | ||
|
||
// new.target !== undefined because we should be called as a new expression | ||
NAPI_CALL(env, napi_strict_equals(env, newTargetArg, undefined, &result)); | ||
NAPI_ASSERT(env, !result, "new.target !== undefined"); | ||
|
||
// arguments[0] should be Constructor itself (test harness passed it) | ||
NAPI_CALL(env, napi_strict_equals(env, newTargetArg, argv, &result)); | ||
NAPI_ASSERT(env, result, "new.target === Constructor"); | ||
|
||
return thisArg; | ||
} | ||
|
||
napi_value OrdinaryFunction(napi_env env, napi_callback_info info) { | ||
bool result; | ||
napi_value newTargetArg; | ||
NAPI_CALL(env, napi_get_new_target(env, info, &newTargetArg)); | ||
napi_value undefined; | ||
NAPI_CALL(env, napi_get_undefined(env, &undefined)); | ||
|
||
// new.target === undefined because we are not called as a new expression | ||
NAPI_CALL(env, napi_strict_equals(env, newTargetArg, undefined, &result)); | ||
NAPI_ASSERT(env, result, "new.target === undefined"); | ||
|
||
napi_value _true; | ||
NAPI_CALL(env, napi_get_boolean(env, true, &_true)); | ||
return _true; | ||
} | ||
|
||
napi_value Init(napi_env env, napi_value exports) { | ||
const napi_property_descriptor desc[] = { | ||
DECLARE_NAPI_PROPERTY("BaseClass", BaseClass), | ||
DECLARE_NAPI_PROPERTY("OrdinaryFunction", OrdinaryFunction), | ||
DECLARE_NAPI_PROPERTY("Constructor", Constructor) | ||
}; | ||
NAPI_CALL(env, napi_define_properties(env, exports, 3, desc)); | ||
return exports; | ||
} | ||
|
||
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ], | ||
'sources': [ 'binding.c' ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const assert = require('assert'); | ||
const binding = require(`./build/${common.buildType}/binding`); | ||
|
||
class Class extends binding.BaseClass { | ||
constructor() { | ||
super(); | ||
this.method(); | ||
} | ||
method() { | ||
this.ok = true; | ||
} | ||
} | ||
|
||
assert.ok(new Class() instanceof binding.BaseClass); | ||
assert.ok(new Class().ok); | ||
assert.ok(binding.OrdinaryFunction()); | ||
assert.ok( | ||
new binding.Constructor(binding.Constructor) instanceof binding.Constructor); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth also having an additional test where new.target === this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it probably is. That would be a regular (non-super) construction case. I'll add one.