forked from nodejs/node-chakracore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
N-API: Implement Promise for ChakraCore
- Loading branch information
Gabriel Schulhof
committed
Jun 16, 2017
1 parent
4f8f708
commit 926b948
Showing
5 changed files
with
173 additions
and
1 deletion.
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
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
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,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_promise", | ||
"sources": [ "test_promise.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,61 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const test_promise = require(`./build/${common.buildType}/test_promise`); | ||
const assert = require('assert'); | ||
|
||
var native_promise, conclude_value; | ||
|
||
// Create a native promise and resolve it. | ||
native_promise = test_promise.napi_create_promise(); | ||
conclude_value = "Promise delivered"; | ||
native_promise.promise | ||
.then( | ||
function( result ) { | ||
assert.strictEqual( result, conclude_value, "Promise was resolved as expected" ); | ||
}, | ||
function( error ) { | ||
assert.ok( false, "Promise was rejected when it should have been resolved: " + | ||
error + ": " + JSON.stringify( error, null, 4 ) ); | ||
} ); | ||
native_promise.resolve( conclude_value ); | ||
|
||
// Make sure the native promise is recognized as a promise. | ||
assert.strictEqual( test_promise.napi_is_promise( native_promise.promise ), true, | ||
"N-API correctly identifies a promise it created as a promise" ); | ||
|
||
// Create a native promise and reject it. | ||
native_promise = test_promise.napi_create_promise(); | ||
conclude_value = new Error( "I broke my promise" ); | ||
native_promise.promise | ||
.then( | ||
function( result ) { | ||
assert.ok( false, "Promise was resolved when it should have been rejected: " + | ||
result + ": " + JSON.stringify( result, null, 4 ) ); | ||
}, | ||
function( error ) { | ||
assert.strictEqual( error, conclude_value, "Promise was rejected as expected" ); | ||
} ); | ||
|
||
// N-API correctly identifies a JS promise as a promise | ||
assert.strictEqual( | ||
test_promise.napi_is_promise( Promise.reject( new Error( "No reason, just sayin'" ) ) ), true, | ||
"N-API correctly identifies a JS promise as a promise" ); | ||
|
||
assert.strictEqual( test_promise.napi_is_promise( 42 ), false, | ||
"N-API correctly reports that a number is not a promise" ); | ||
|
||
assert.strictEqual( test_promise.napi_is_promise( "Gumball" ), false, | ||
"N-API correctly reports that a string is not a promise" ); | ||
|
||
assert.strictEqual( test_promise.napi_is_promise( undefined ), false, | ||
"N-API correctly reports that undefined is not a promise" ); | ||
|
||
assert.strictEqual( test_promise.napi_is_promise( null ), false, | ||
"N-API correctly reports that null is not a promise" ); | ||
|
||
assert.strictEqual( test_promise.napi_is_promise( {} ), false, | ||
"N-API correctly reports that an object is not a promise" ); | ||
|
||
assert.strictEqual( test_promise.napi_is_promise( Symbol( "Espoo" ) ), false, | ||
"N-API correctly reports that a symbol is not a promise" ); |
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,50 @@ | ||
#include <stdio.h> | ||
#include <node_api.h> | ||
#include "../common.h" | ||
|
||
napi_value bind_napi_create_promise(napi_env env, napi_callback_info info) { | ||
napi_value promise, resolve, reject, result; | ||
|
||
NAPI_CALL(env, napi_create_promise(env, &promise, &resolve, &reject)); | ||
NAPI_CALL(env, napi_create_object(env, &result)); | ||
|
||
fprintf(stderr, "resolve: %p\n", resolve); | ||
|
||
napi_valuetype value_type; | ||
NAPI_CALL(env, napi_typeof(env, resolve, &value_type)); | ||
fprintf(stderr, "resolve: value_type: %d\n", value_type); | ||
|
||
napi_property_descriptor properties[] = { | ||
{ "promise", NULL, NULL, NULL, NULL, promise, napi_default, NULL }, | ||
{ "resolve", NULL, NULL, NULL, NULL, resolve, napi_default, NULL }, | ||
{ "reject", NULL, NULL, NULL, NULL, reject, napi_default, NULL }, | ||
}; | ||
|
||
NAPI_CALL(env, napi_define_properties(env, result, 3, properties)); | ||
|
||
return result; | ||
} | ||
|
||
napi_value bind_napi_is_promise(napi_env env, napi_callback_info info) { | ||
napi_value promise, result; | ||
size_t argc = 1; | ||
bool is_promise; | ||
|
||
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &promise, NULL, NULL)); | ||
NAPI_CALL(env, napi_is_promise(env, promise, &is_promise)); | ||
NAPI_CALL(env, napi_get_boolean(env, is_promise, &result)); | ||
|
||
return result; | ||
} | ||
|
||
void Init(napi_env env, napi_value exports, napi_value module, void* priv) { | ||
napi_property_descriptor descriptors[] = { | ||
DECLARE_NAPI_PROPERTY("napi_create_promise", bind_napi_create_promise), | ||
DECLARE_NAPI_PROPERTY("napi_is_promise", bind_napi_is_promise) | ||
}; | ||
|
||
NAPI_CALL_RETURN_VOID(env, napi_define_properties( | ||
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors)); | ||
} | ||
|
||
NAPI_MODULE(addon, Init) |