Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add typing to most examples. #538

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"semver": "^7.1.3"
},
"devDependencies": {
"@types/bindings": "^1.5.5",
"husky": "^4.3.0",
"lint-staged": "^10.5.2"
}
Expand Down
1 change: 1 addition & 0 deletions src/1-getting-started/1_hello_world/nan/hello.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('../type')} */
var addon = require('bindings')('hello');

console.log(addon.hello()); // 'world'
1 change: 1 addition & 0 deletions src/1-getting-started/1_hello_world/napi/hello.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('../type')} */
var addon = require('bindings')('hello');

console.log(addon.hello()); // 'world'
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('../type')} */
var addon = require('bindings')('hello');

console.log(addon.hello()); // 'world'
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('../type')} */
var addon = require('bindings')('hello');

console.log(addon.hello()); // 'world'
4 changes: 4 additions & 0 deletions src/1-getting-started/1_hello_world/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Returns the string "world".
*/
export function hello(): "world";
1 change: 1 addition & 0 deletions src/1-getting-started/2_function_arguments/nan/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('../type')} */
var addon = require('bindings')('addon.node')

console.log('This should be eight:', addon.add(3, 5))
1 change: 1 addition & 0 deletions src/1-getting-started/2_function_arguments/napi/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @type {import('../type')} */
var addon = require('bindings')('addon.node')

console.log('This should be eight:', addon.add(3, 5))
6 changes: 6 additions & 0 deletions src/1-getting-started/2_function_arguments/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* add two digits
* @param a first digit
* @param b second digit
*/
export function add(a: number, b: number): number;
3 changes: 3 additions & 0 deletions src/1-getting-started/3_callbacks/nan/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var addon = require('bindings')('addon');

addon(function(msg){
Expand Down
3 changes: 3 additions & 0 deletions src/1-getting-started/3_callbacks/napi/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var addon = require('bindings')('addon');

addon(function(msg){
Expand Down
3 changes: 3 additions & 0 deletions src/1-getting-started/3_callbacks/node-addon-api/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var addon = require('bindings')('addon');

addon(function(msg){
Expand Down
1 change: 1 addition & 0 deletions src/1-getting-started/3_callbacks/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function addon(callback: (msg: "hello world") => void): void;
3 changes: 3 additions & 0 deletions src/1-getting-started/4_object_factory/nan/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var addon = require('bindings')('addon');

var obj1 = addon('hello');
Expand Down
3 changes: 3 additions & 0 deletions src/1-getting-started/4_object_factory/napi/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var addon = require('bindings')('addon');

var obj1 = addon('hello');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var addon = require('bindings')('addon');

var obj1 = addon('hello');
Expand Down
4 changes: 4 additions & 0 deletions src/1-getting-started/4_object_factory/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Returns the string "world".
*/
export default function hello(msg: string): { msg: string };
3 changes: 3 additions & 0 deletions src/1-getting-started/5_function_factory/nan/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var addon = require('bindings')('addon');

var fn = addon();
Expand Down
3 changes: 3 additions & 0 deletions src/1-getting-started/5_function_factory/napi/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var addon = require('bindings')('addon');

var fn = addon();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var addon = require('bindings')('addon');

var fn = addon();
Expand Down
4 changes: 4 additions & 0 deletions src/1-getting-started/5_function_factory/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Returns the string "world".
*/
export default function factory(): ()=> "hello world";
3 changes: 3 additions & 0 deletions src/1-getting-started/6_object_wrap/nan/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type')}
*/
var addon = require('bindings')('addon');

var obj = new addon.MyObject(10);
Expand Down
3 changes: 3 additions & 0 deletions src/1-getting-started/6_object_wrap/napi/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('./type')}
*/
var addon = require('bindings')('addon');

var obj = new addon.MyObject(10);
Expand Down
7 changes: 7 additions & 0 deletions src/1-getting-started/6_object_wrap/napi/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class MyObject{
public value: number;

constructor(num: number);
public plusOne(): number;
public multiply(num?: number): MyObject;
}
3 changes: 3 additions & 0 deletions src/1-getting-started/6_object_wrap/node-addon-api/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type')}
*/
var addon = require('bindings')('addon');

var obj = new addon.MyObject(10);
Expand Down
6 changes: 6 additions & 0 deletions src/1-getting-started/6_object_wrap/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export declare class MyObject{
constructor(num: number);
public value(): number;
public plusOne(): number;
public multiply(num?: number): MyObject;
}
3 changes: 3 additions & 0 deletions src/1-getting-started/7_factory_wrap/nan/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var createObject = require('bindings')('addon');

var obj = createObject(10);
Expand Down
3 changes: 3 additions & 0 deletions src/1-getting-started/7_factory_wrap/napi/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var createObject = require('bindings')('addon');

var obj = createObject(10);
Expand Down
3 changes: 3 additions & 0 deletions src/1-getting-started/7_factory_wrap/node-addon-api/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type').default}
*/
var createObject = require('bindings')('addon');

var obj = createObject(10);
Expand Down
3 changes: 3 additions & 0 deletions src/1-getting-started/7_factory_wrap/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function createObject(number: number): {
plusOne(): number;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('./type')}
*/
const addon = require('../build/Release/hello-world-native');

module.exports = addon.HelloWorld
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Returns the string "world".
*/
export function HelloWorld(notused?: unknown): "world";
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ assert(HelloWorld, "The expected function is undefined");

function testBasic()
{
const result = HelloWorld("hello");
const result = HelloWorld("hello");
assert.strictEqual(result, "world", "Unexpected value returned");
}

Expand Down
3 changes: 3 additions & 0 deletions src/2-js-to-native-conversion/8_passing_wrapped/nan/addon.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type')}
*/
var addon = require('bindings')('addon');

var obj1 = addon.createObject(10);
Expand Down
4 changes: 4 additions & 0 deletions src/2-js-to-native-conversion/8_passing_wrapped/napi/addon.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/**
* @type {import('../type')}
*/
var addon = require('bindings')('addon');


var obj1 = addon.createObject(10);
var obj2 = addon.createObject(20);
var result = addon.add(obj1, obj2);
Expand Down
9 changes: 9 additions & 0 deletions src/2-js-to-native-conversion/8_passing_wrapped/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class MyObject {
}

export function createObject(num: number): MyObject;
export function add(obj1: MyObject, obj2: MyObject): number;
/**
* imported path ending by .node
*/
export const path: string;
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('./type')}
*/
const binding = require('bindings')('array_buffer_to_native');
const array = new Int32Array(10);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* dump a buffer to the console
* @param buffer buffer to dump
*/
export function AcceptArrayBuffer(buffer: ArrayBufferLike): void;
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type')}
*/
const addon = require('bindings')('object-template-demo');

const interceptor = addon.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('../type')}
*/
const addon = require('bindings')('object_template_demo');

const interceptor = addon.create();
Expand Down
6 changes: 6 additions & 0 deletions src/2-js-to-native-conversion/object-template-demo/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* proxy object template
*/
export type ObjectTemplate = any;

export function create() : ObjectTemplate;
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/**
* @type {import('./type')}
*/
const addon = require('../build/Release/object-wrap-demo-native');

/**
*
* @param {string} name
*/
function ObjectWrapDemo(name) {
this.greet = function(str) {
return _addonInstance.greet(str);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class ObjectWrapDemo {
constructor(name: string);
/**
* return a string like "Hello, ${str}\nI am ${name}\n"
* @param str greeting string
*/
greet(str: string): string;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* @type {import('./type')}
*/
const binding = require('bindings')('typed_array_to_native');

{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export function AcceptByteArray(array: Uint8Array): undefined;
export function CreateByteArray(array: number[]): Uint8Array;
13 changes: 11 additions & 2 deletions src/3-context-awareness/napi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ const {
} = require('worker_threads');

// We load the native addon.
/**
* @type {import('../type')}
*/
const addon = require('bindings')('multiple_load');

// The iteration count can be tweaked to ensure that the output from the two
// threads is interleaved. Too few iterations and the output of one thread
// follows the output of the other, not really illustrating the concurrency.
const iterations = 1000;

// This function is an idle loop that performs a random walk from 0 by calling
// into the native addon to either increment or decrement the initial value.
/**
* This function is an idle loop that performs a random walk from 0 by calling
* into the native addon to either increment or decrement the initial value.
*
* @param {import('../type')} addon
* @param {string} prefix
* @param {number} iterations
*/
function useAddon(addon, prefix, iterations) {
if (iterations >= 0) {
if (Math.random() < 0.5) {
Expand Down
13 changes: 11 additions & 2 deletions src/3-context-awareness/node_10/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ const {
} = require('worker_threads');

// We load the native addon.
/**
* @type {import('../type')}
*/
const addon = require('bindings')('multiple_load');

// The iteration count can be tweaked to ensure that the output from the two
// threads is interleaved. Too few iterations and the output of one thread
// follows the output of the other, not really illustrating the concurrency.
const iterations = 1000;

// This function is an idle loop that performs a random walk from 0 by calling
// into the native addon to either increment or decrement the initial value.
/**
* This function is an idle loop that performs a random walk from 0 by calling
* into the native addon to either increment or decrement the initial value.
*
* @param {import('../type')} addon
* @param {string} prefix
* @param {number} iterations
*/
function useAddon(addon, prefix, iterations) {
if (iterations >= 0) {
if (Math.random() < 0.5) {
Expand Down
8 changes: 8 additions & 0 deletions src/3-context-awareness/type.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Decrement the counter with a CPU-intensif calculation.
*/
export function decrement(): number;
/**
* Increment the counter with a CPU-intensif calculation.
*/
export function increment(): number;
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict'

/**
* @type {import('./type')}
*/
const { NativeAddon } = require('bindings')('addon')

function JSFnRef() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export declare class NativeAddon {
constructor(JSFnRef: () => void, JSFn: () => void);
/**
* call the JSFn by the stored reference
*/
tryCallByStoredReference(): void;
/**
* call the JSFn directly
*/
tryCallByStoredFunction(): void;
}
Loading