Skip to content

Commit 90289b3

Browse files
committed
Update addon snippets
1 parent 1948e47 commit 90289b3

File tree

4 files changed

+175
-70
lines changed

4 files changed

+175
-70
lines changed

tools/snippets/src/Makefile

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#/
2+
# @license Apache-2.0
3+
#
4+
# Copyright (c) 2022 The Stdlib Authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#/
18+
19+
# VARIABLES #
20+
21+
ifndef VERBOSE
22+
QUIET := @
23+
else
24+
QUIET :=
25+
endif
26+
27+
# Determine the OS ([1][1], [2][2]).
28+
#
29+
# [1]: https://en.wikipedia.org/wiki/Uname#Examples
30+
# [2]: http://stackoverflow.com/a/27776822/2225624
31+
OS ?= $(shell uname)
32+
ifneq (, $(findstring MINGW,$(OS)))
33+
OS := WINNT
34+
else
35+
ifneq (, $(findstring MSYS,$(OS)))
36+
OS := WINNT
37+
else
38+
ifneq (, $(findstring CYGWIN,$(OS)))
39+
OS := WINNT
40+
else
41+
ifneq (, $(findstring Windows_NT,$(OS)))
42+
OS := WINNT
43+
endif
44+
endif
45+
endif
46+
endif
47+
48+
49+
# RULES #
50+
51+
#/
52+
# Removes generated files for building an add-on.
53+
#
54+
# @example
55+
# make clean-addon
56+
#/
57+
clean-addon:
58+
$(QUIET) -rm -f *.o *.node
59+
60+
.PHONY: clean-addon
61+
62+
#/
63+
# Removes generated files.
64+
#
65+
# @example
66+
# make clean
67+
#/
68+
clean: clean-addon
69+
70+
.PHONY: clean

tools/snippets/src/addon.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "TODO.h"
20+
#include <node_api.h>
21+
#include <stdint.h>
22+
#include <stdbool.h>
23+
#include <assert.h>
24+
25+
/**
26+
* Receives JavaScript callback invocation data.
27+
*
28+
* @private
29+
* @param env environment under which the function is invoked
30+
* @param info callback data
31+
* @return Node-API value
32+
*/
33+
static napi_value addon( napi_env env, napi_callback_info info ) {
34+
napi_status status;
35+
36+
// Get callback arguments:
37+
size_t argc = 2;
38+
napi_value argv[ 2 ];
39+
status = napi_get_cb_info( env, info, &argc, argv, NULL, NULL );
40+
assert( status == napi_ok );
41+
42+
// Check whether we were provided the correct number of arguments:
43+
if ( argc < 2 ) {
44+
status = napi_throw_error( env, NULL, "invalid invocation. Insufficient arguments." );
45+
assert( status == napi_ok );
46+
return NULL;
47+
}
48+
if ( argc > 2 ) {
49+
status = napi_throw_error( env, NULL, "invalid invocation. Too many arguments." );
50+
assert( status == napi_ok );
51+
return NULL;
52+
}
53+
54+
napi_valuetype vtype0;
55+
status = napi_typeof( env, argv[ 0 ], &vtype0 );
56+
assert( status == napi_ok );
57+
if ( vtype0 != napi_number ) {
58+
status = napi_throw_type_error( env, NULL, "invalid argument. First argument must be a number." );
59+
assert( status == napi_ok );
60+
return NULL;
61+
}
62+
63+
napi_valuetype vtype1;
64+
status = napi_typeof( env, argv[ 1 ], &vtype1 );
65+
assert( status == napi_ok );
66+
if ( vtype1 != napi_number ) {
67+
status = napi_throw_type_error( env, NULL, "invalid argument. Second argument must be a number." );
68+
assert( status == napi_ok );
69+
return NULL;
70+
}
71+
72+
double x;
73+
status = napi_get_value_double( env, argv[ 0 ], &x );
74+
assert( status == napi_ok );
75+
76+
double y;
77+
status = napi_get_value_double( env, argv[ 1 ], &y );
78+
assert( status == napi_ok );
79+
80+
double z = stdlib_TODO( x, y );
81+
82+
napi_value v;
83+
status = napi_create_double( env, z, &v );
84+
assert( status == napi_ok );
85+
86+
return v;
87+
}
88+
89+
/**
90+
* Initializes a Node-API module.
91+
*
92+
* @private
93+
* @param env environment under which the function is invoked
94+
* @param exports exports object
95+
* @return main export
96+
*/
97+
static napi_value init( napi_env env, napi_value exports ) {
98+
napi_value fcn;
99+
napi_status status = napi_create_function( env, "exports", NAPI_AUTO_LENGTH, addon, NULL, &fcn );
100+
assert( status == napi_ok );
101+
return fcn;
102+
}
103+
104+
NAPI_MODULE( NODE_GYP_MODULE_NAME, init )

tools/snippets/src/addon.cpp

Lines changed: 0 additions & 69 deletions
This file was deleted.

tools/snippets/src/todo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include <math.h>
2019
#include "TODO.h"
20+
#include <math.h>
2121

2222
/**
2323
* TODO.

0 commit comments

Comments
 (0)