Skip to content

Commit bea19f9

Browse files
committed
Add a C API for returning an ndarray dtype from a (JavaScript) dtype string
1 parent 97d6774 commit bea19f9

File tree

10 files changed

+540
-0
lines changed

10 files changed

+540
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# N-API ndarray dtype
22+
23+
> C API for returning the ndarray [data type][@stdlib/ndarray/dtypes] corresponding to a data type string.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```c
40+
#include "stdlib/ndarray/base/napi/dtype_string_to_dtype.h"
41+
```
42+
43+
#### stdlib_ndarray_napi_dtype_string_to_dtype( const char \*str )
44+
45+
Returns the ndarray [data type][@stdlib/ndarray/dtypes] corresponding to a data type string.
46+
47+
```cpp
48+
#include "stdlib/ndarray/base/napi/dtype_string_to_dtype.h"
49+
#include "stdlib/ndarray/dtypes.h"
50+
#include <node_api.h>
51+
#include <assert.h>
52+
53+
// Add-on function export...
54+
napi_value addon( napi_env env, napi_callback_info info ) {
55+
56+
// ...
57+
58+
// Get function arguments...
59+
size_t argc = 1;
60+
napi_value argv[ 1 ];
61+
napi_status status = napi_get_cb_info( env, info, &argc, argv, nullptr, nullptr );
62+
assert( status == napi_ok );
63+
64+
// ...
65+
66+
// Get a string argument...
67+
char str[ 1024 ];
68+
size_t len;
69+
status = napi_get_value_string_utf8( env, argv[ 0 ], (char *)str, 1024, &len );
70+
assert( status == napi_ok );
71+
72+
// ...
73+
74+
// Return the corresponding ndarray data type for the input typed array:
75+
enum STDLIB_NDARRAY_DTYPE dtype = stdlib_ndarray_napi_dtype_string_to_dtype( str );
76+
77+
// ...
78+
79+
}
80+
```
81+
82+
</section>
83+
84+
<!-- /.usage -->
85+
86+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
87+
88+
<section class="notes">
89+
90+
</section>
91+
92+
<!-- /.notes -->
93+
94+
<!-- Package usage examples. -->
95+
96+
<section class="examples">
97+
98+
</section>
99+
100+
<!-- /.examples -->
101+
102+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
103+
104+
<section class="references">
105+
106+
</section>
107+
108+
<!-- /.references -->
109+
110+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
111+
112+
<section class="links">
113+
114+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
115+
116+
</section>
117+
118+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
#ifndef STDLIB_NDARRAY_BASE_NAPI_DTYPE_STRING_TO_DTYPE_H
20+
#define STDLIB_NDARRAY_BASE_NAPI_DTYPE_STRING_TO_DTYPE_H
21+
22+
#include "stdlib/ndarray/dtypes.h"
23+
#include <node_api.h>
24+
#include <stdint.h>
25+
26+
/*
27+
* If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler.
28+
*/
29+
#ifdef __cplusplus
30+
extern "C" {
31+
#endif
32+
33+
/**
34+
* Returns the ndarray data type corresponding to a data type string.
35+
*/
36+
enum STDLIB_NDARRAY_DTYPE stdlib_ndarray_napi_dtype_string_to_dtype( const char *str );
37+
38+
#ifdef __cplusplus
39+
}
40+
#endif
41+
42+
#endif // !STDLIB_NDARRAY_BASE_NAPI_DTYPE_STRING_TO_DTYPE_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
'use strict';
20+
21+
// MAIN //
22+
23+
var dir = null;
24+
25+
26+
// EXPORTS //
27+
28+
module.exports = dir;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
'use strict';
20+
21+
/**
22+
* Absolute file path for the directory containing header files for C APIs.
23+
*
24+
* @module @stdlib/ndarray/base/napi/dtype-string-to-dtype
25+
*
26+
* @example
27+
* var headerDir = require( '@stdlib/ndarray/base/napi/dtype-string-to-dtype' );
28+
*
29+
* console.log( headerDir );
30+
*/
31+
32+
// MODULES //
33+
34+
var dir = require( './main.js' );
35+
36+
37+
// EXPORTS //
38+
39+
module.exports = dir;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
'use strict';
20+
21+
// MODULES //
22+
23+
var resolve = require( 'path' ).resolve;
24+
25+
26+
// MAIN //
27+
28+
var dir = resolve( __dirname, '..', 'include' );
29+
30+
31+
// EXPORTS //
32+
33+
module.exports = dir;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"options": {},
3+
"fields": [
4+
{
5+
"field": "src",
6+
"resolve": true,
7+
"relative": true
8+
},
9+
{
10+
"field": "include",
11+
"resolve": true,
12+
"relative": true
13+
},
14+
{
15+
"field": "libraries",
16+
"resolve": false,
17+
"relative": false
18+
},
19+
{
20+
"field": "libpath",
21+
"resolve": true,
22+
"relative": false
23+
}
24+
],
25+
"confs": [
26+
{
27+
"src": [
28+
"./src/main.c"
29+
],
30+
"include": [
31+
"./include"
32+
],
33+
"libraries": [],
34+
"libpath": [],
35+
"dependencies": [
36+
"@stdlib/ndarray/dtypes"
37+
]
38+
}
39+
]
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"name": "@stdlib/ndarray/base/napi/dtype-string-to-dtype",
3+
"version": "0.0.0",
4+
"description": "C API for returning the ndarray data type corresponding to a data type string.",
5+
"license": "Apache-2.0",
6+
"author": {
7+
"name": "The Stdlib Authors",
8+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9+
},
10+
"contributors": [
11+
{
12+
"name": "The Stdlib Authors",
13+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14+
}
15+
],
16+
"main": "./lib",
17+
"browser": "./lib/browser.js",
18+
"directories": {
19+
"include": "./include",
20+
"lib": "./lib",
21+
"src": "./src",
22+
"test": "./test"
23+
},
24+
"scripts": {},
25+
"homepage": "https://github.com/stdlib-js/stdlib",
26+
"repository": {
27+
"type": "git",
28+
"url": "git://github.com/stdlib-js/stdlib.git"
29+
},
30+
"bugs": {
31+
"url": "https://github.com/stdlib-js/stdlib/issues"
32+
},
33+
"dependencies": {},
34+
"devDependencies": {},
35+
"engines": {
36+
"node": ">=0.10.0",
37+
"npm": ">2.7.0"
38+
},
39+
"os": [
40+
"aix",
41+
"darwin",
42+
"freebsd",
43+
"linux",
44+
"macos",
45+
"openbsd",
46+
"sunos",
47+
"win32",
48+
"windows"
49+
],
50+
"keywords": [
51+
"stdlib",
52+
"stdtypes",
53+
"types",
54+
"base",
55+
"ndarray",
56+
"napi",
57+
"dtype",
58+
"string"
59+
],
60+
"__stdlib__": {}
61+
}

0 commit comments

Comments
 (0)