2023-09-20 17:18:01 +09:00
|
|
|
import { exec } from 'node:child_process'
|
|
|
|
import { join } from 'node:path'
|
|
|
|
import { fileURLToPath } from 'node:url'
|
2021-10-25 01:00:31 +09:00
|
|
|
|
2024-03-20 22:37:08 +09:00
|
|
|
import { Subject, take } from 'rxjs'
|
2022-04-14 01:02:06 +09:00
|
|
|
import { spy } from 'sinon'
|
2021-09-23 02:29:09 +09:00
|
|
|
|
2024-02-26 16:46:07 +09:00
|
|
|
import {
|
2021-11-10 20:04:36 +09:00
|
|
|
DEFAULT_COST,
|
2021-09-23 02:29:09 +09:00
|
|
|
add,
|
|
|
|
fibonacci,
|
2024-01-26 00:28:57 +09:00
|
|
|
call0,
|
|
|
|
call1,
|
|
|
|
call2,
|
|
|
|
apply0,
|
|
|
|
apply1,
|
|
|
|
callFunction,
|
|
|
|
callFunctionWithArg,
|
|
|
|
callFunctionWithArgAndCtx,
|
|
|
|
createReferenceOnFunction,
|
|
|
|
referenceAsCallback,
|
2021-09-23 02:29:09 +09:00
|
|
|
contains,
|
|
|
|
concatLatin1,
|
|
|
|
concatStr,
|
|
|
|
concatUtf16,
|
2022-02-04 17:29:17 +09:00
|
|
|
roundtripStr,
|
2021-09-23 02:29:09 +09:00
|
|
|
getNums,
|
|
|
|
getWords,
|
|
|
|
sumNums,
|
2021-12-19 00:16:09 +09:00
|
|
|
getMapping,
|
|
|
|
sumMapping,
|
2024-02-15 16:43:21 +09:00
|
|
|
getBtreeMapping,
|
|
|
|
sumBtreeMapping,
|
|
|
|
getIndexMapping,
|
|
|
|
sumIndexMapping,
|
|
|
|
indexmapPassthrough,
|
2021-09-23 02:29:09 +09:00
|
|
|
getCwd,
|
|
|
|
Animal,
|
|
|
|
Kind,
|
2022-07-06 00:09:40 +09:00
|
|
|
NinjaTurtle,
|
2021-11-05 19:31:36 +09:00
|
|
|
ClassWithFactory,
|
2021-09-23 02:29:09 +09:00
|
|
|
CustomNumEnum,
|
2021-11-06 22:48:18 +09:00
|
|
|
Context,
|
2023-03-21 12:22:07 +09:00
|
|
|
GetterSetterWithClosures,
|
2021-09-23 02:29:09 +09:00
|
|
|
enumToI32,
|
|
|
|
listObjKeys,
|
|
|
|
createObj,
|
|
|
|
mapOption,
|
2021-09-24 10:15:48 +09:00
|
|
|
readFile,
|
2021-09-24 15:45:27 +09:00
|
|
|
throwError,
|
2023-02-10 00:18:57 +09:00
|
|
|
customStatusCode,
|
2022-08-20 00:36:36 +09:00
|
|
|
panic,
|
2021-09-24 18:01:54 +09:00
|
|
|
readPackageJson,
|
2021-09-28 01:01:19 +09:00
|
|
|
getPackageJsonName,
|
2021-10-08 22:13:27 +09:00
|
|
|
getBuffer,
|
2022-04-23 17:43:36 +09:00
|
|
|
getEmptyBuffer,
|
2024-01-26 15:00:52 +09:00
|
|
|
asyncBufferToArray,
|
2021-10-25 01:00:31 +09:00
|
|
|
readFileAsync,
|
2021-11-02 01:34:19 +09:00
|
|
|
eitherStringOrNumber,
|
|
|
|
returnEither,
|
|
|
|
either3,
|
|
|
|
either4,
|
2021-11-02 21:36:34 +09:00
|
|
|
withoutAbortController,
|
|
|
|
withAbortController,
|
2021-11-06 23:19:42 +09:00
|
|
|
asyncMultiTwo,
|
2021-11-11 01:33:51 +09:00
|
|
|
bigintAdd,
|
|
|
|
createBigInt,
|
|
|
|
createBigIntI64,
|
2022-02-04 17:03:32 +09:00
|
|
|
bigintGetU64AsString,
|
2021-11-12 18:22:57 +09:00
|
|
|
callThreadsafeFunction,
|
|
|
|
threadsafeFunctionThrowError,
|
2022-08-20 23:40:26 +09:00
|
|
|
threadsafeFunctionClosureCapture,
|
2023-01-11 19:54:45 +09:00
|
|
|
tsfnCallWithCallback,
|
|
|
|
tsfnAsyncCall,
|
2023-04-10 18:02:13 +09:00
|
|
|
tsfnThrowFromJs,
|
2021-11-13 21:51:14 +09:00
|
|
|
asyncPlus100,
|
2021-11-15 19:56:06 +09:00
|
|
|
getGlobal,
|
|
|
|
getUndefined,
|
|
|
|
getNull,
|
2021-11-16 00:09:44 +09:00
|
|
|
setSymbolInObj,
|
|
|
|
createSymbol,
|
2023-09-14 07:45:14 +09:00
|
|
|
createSymbolFor,
|
2021-11-21 15:49:04 +09:00
|
|
|
threadsafeFunctionFatalMode,
|
2021-11-21 17:11:15 +09:00
|
|
|
createExternal,
|
|
|
|
getExternal,
|
|
|
|
mutateExternal,
|
|
|
|
createExternalString,
|
2021-11-23 20:00:31 +09:00
|
|
|
xxh2,
|
|
|
|
xxh3,
|
|
|
|
xxh64Alias,
|
2021-11-26 18:26:14 +09:00
|
|
|
tsRename,
|
2024-02-16 00:40:45 +09:00
|
|
|
acceptSlice,
|
|
|
|
u8ArrayToArray,
|
|
|
|
i8ArrayToArray,
|
|
|
|
u16ArrayToArray,
|
|
|
|
i16ArrayToArray,
|
|
|
|
u32ArrayToArray,
|
|
|
|
i32ArrayToArray,
|
|
|
|
u64ArrayToArray,
|
|
|
|
i64ArrayToArray,
|
|
|
|
f32ArrayToArray,
|
|
|
|
f64ArrayToArray,
|
2024-02-25 02:00:28 +09:00
|
|
|
acceptUint8ClampedSlice,
|
|
|
|
acceptUint8ClampedSliceAndBufferSlice,
|
2021-11-30 19:43:34 +09:00
|
|
|
convertU32Array,
|
|
|
|
createExternalTypedArray,
|
|
|
|
mutateTypedArray,
|
2021-12-02 16:47:59 +09:00
|
|
|
receiveAllOptionalObject,
|
2024-02-28 15:29:02 +09:00
|
|
|
objectGetNamedPropertyShouldPerformTypecheck,
|
2021-12-02 17:25:20 +09:00
|
|
|
fnReceivedAliased,
|
|
|
|
ALIAS,
|
2021-12-18 14:24:16 +09:00
|
|
|
appendBuffer,
|
2021-12-18 14:36:45 +09:00
|
|
|
returnNull,
|
|
|
|
returnUndefined,
|
2021-12-23 15:07:35 +09:00
|
|
|
Dog,
|
2021-12-24 19:46:10 +09:00
|
|
|
Bird,
|
2021-12-25 18:35:47 +09:00
|
|
|
Assets,
|
2022-01-06 16:56:14 +09:00
|
|
|
receiveStrictObject,
|
2022-01-06 17:16:43 +09:00
|
|
|
receiveClassOrNumber,
|
|
|
|
JsClassForEither,
|
|
|
|
receiveMutClassOrNumber,
|
2022-01-12 16:19:05 +09:00
|
|
|
getStrFromObject,
|
2022-01-23 19:17:00 +09:00
|
|
|
returnJsFunction,
|
2022-02-06 12:58:17 +09:00
|
|
|
testSerdeRoundtrip,
|
2023-03-23 14:34:34 +09:00
|
|
|
testSerdeBigNumberPrecision,
|
2022-02-06 17:25:32 +09:00
|
|
|
createObjWithProperty,
|
2023-01-24 15:51:16 +09:00
|
|
|
receiveObjectOnlyFromJs,
|
2022-02-06 21:42:48 +09:00
|
|
|
dateToNumber,
|
2024-04-30 17:12:30 +09:00
|
|
|
chronoUtcDateToMillis,
|
|
|
|
chronoLocalDateToMillis,
|
|
|
|
chronoDateWithTimezoneToMillis,
|
|
|
|
chronoDateFixtureReturn1,
|
|
|
|
chronoDateFixtureReturn2,
|
2022-02-07 13:24:09 +09:00
|
|
|
derefUint8Array,
|
2022-02-10 04:50:46 +09:00
|
|
|
chronoDateAdd1Minute,
|
2022-02-28 02:26:48 +09:00
|
|
|
bufferPassThrough,
|
2022-07-06 20:08:34 +09:00
|
|
|
arrayBufferPassThrough,
|
2022-03-21 17:36:06 +09:00
|
|
|
JsRepo,
|
2024-02-22 23:37:50 +09:00
|
|
|
JsRemote,
|
2022-04-26 19:06:52 +09:00
|
|
|
CssStyleSheet,
|
2023-12-27 13:49:10 +09:00
|
|
|
CatchOnConstructor,
|
|
|
|
CatchOnConstructor2,
|
2022-04-06 22:08:00 +09:00
|
|
|
asyncReduceBuffer,
|
2022-04-14 01:02:06 +09:00
|
|
|
callbackReturnPromise,
|
2023-05-16 12:05:37 +09:00
|
|
|
callbackReturnPromiseAndSpawn,
|
2022-05-12 16:11:47 +09:00
|
|
|
returnEitherClass,
|
|
|
|
eitherFromOption,
|
2022-08-07 02:16:28 +09:00
|
|
|
eitherFromObjects,
|
2022-05-22 14:43:11 +09:00
|
|
|
overrideIndividualArgOnFunction,
|
|
|
|
overrideIndividualArgOnFunctionWithCbArg,
|
2022-06-04 02:07:39 +09:00
|
|
|
createObjectWithClassField,
|
|
|
|
receiveObjectWithClassField,
|
2022-07-05 18:00:45 +09:00
|
|
|
AnotherClassForEither,
|
|
|
|
receiveDifferentClass,
|
2022-08-11 23:47:13 +09:00
|
|
|
getNumArr,
|
|
|
|
getNestedNumArr,
|
2022-08-17 14:24:40 +09:00
|
|
|
CustomFinalize,
|
2022-08-17 18:49:22 +09:00
|
|
|
plusOne,
|
|
|
|
Width,
|
2022-09-14 20:30:43 +09:00
|
|
|
captureErrorInCallback,
|
2023-01-17 01:05:19 +09:00
|
|
|
bigintFromI128,
|
|
|
|
bigintFromI64,
|
2023-01-24 15:25:05 +09:00
|
|
|
acceptThreadsafeFunction,
|
|
|
|
acceptThreadsafeFunctionFatal,
|
2023-02-08 23:30:12 +09:00
|
|
|
acceptThreadsafeFunctionTupleArgs,
|
2023-01-24 20:07:33 +09:00
|
|
|
promiseInEither,
|
2023-01-31 21:36:59 +09:00
|
|
|
runScript,
|
2023-03-14 16:32:17 +09:00
|
|
|
tsfnReturnPromise,
|
|
|
|
tsfnReturnPromiseTimeout,
|
2023-03-21 19:12:52 +09:00
|
|
|
returnFromSharedCrate,
|
2023-05-26 19:28:34 +09:00
|
|
|
chronoNativeDateTime,
|
|
|
|
chronoNativeDateTimeReturn,
|
2023-07-15 13:07:14 +09:00
|
|
|
throwAsyncError,
|
2023-11-06 12:03:44 +09:00
|
|
|
getModuleFileName,
|
|
|
|
throwSyntaxError,
|
2024-02-26 16:46:07 +09:00
|
|
|
type AliasedStruct,
|
2024-03-10 22:22:46 +09:00
|
|
|
returnObjectOnlyToJs,
|
2024-03-20 22:37:08 +09:00
|
|
|
buildThreadsafeFunctionFromFunction,
|
2024-04-15 18:54:10 +09:00
|
|
|
createOptionalExternal,
|
|
|
|
getOptionalExternal,
|
|
|
|
mutateOptionalExternal,
|
2024-04-18 00:34:47 +09:00
|
|
|
panicInAsync,
|
2024-02-26 16:46:07 +09:00
|
|
|
} from '../index.cjs'
|
|
|
|
|
|
|
|
import { test } from './test.framework.js'
|
|
|
|
|
|
|
|
const __dirname = join(fileURLToPath(import.meta.url), '..')
|
2023-11-02 13:57:11 +09:00
|
|
|
|
|
|
|
const Napi4Test = Number(process.versions.napi) >= 4 ? test : test.skip
|
2021-09-23 02:29:09 +09:00
|
|
|
|
2021-11-10 20:04:36 +09:00
|
|
|
test('export const', (t) => {
|
|
|
|
t.is(DEFAULT_COST, 12)
|
|
|
|
})
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
test('number', (t) => {
|
|
|
|
t.is(add(1, 2), 3)
|
|
|
|
t.is(fibonacci(5), 5)
|
|
|
|
|
|
|
|
t.throws(
|
|
|
|
// @ts-expect-error
|
|
|
|
() => fibonacci(''),
|
2022-01-04 12:04:45 +09:00
|
|
|
void 0,
|
2021-09-23 02:29:09 +09:00
|
|
|
'Expect value to be Number, but received String',
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('string', (t) => {
|
|
|
|
t.true(contains('hello', 'ell'))
|
|
|
|
t.false(contains('John', 'jn'))
|
|
|
|
|
|
|
|
t.is(concatStr('涽¾DEL'), '涽¾DEL + Rust 🦀 string!')
|
|
|
|
t.is(concatLatin1('涽¾DEL'), '涽¾DEL + Rust 🦀 string!')
|
|
|
|
t.is(
|
|
|
|
concatUtf16('JavaScript 🌳 你好 napi'),
|
|
|
|
'JavaScript 🌳 你好 napi + Rust 🦀 string!',
|
|
|
|
)
|
2022-02-04 17:29:17 +09:00
|
|
|
t.is(
|
|
|
|
roundtripStr('what up?!\u0000after the NULL'),
|
|
|
|
'what up?!\u0000after the NULL',
|
|
|
|
)
|
2021-09-23 02:29:09 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
test('array', (t) => {
|
|
|
|
t.deepEqual(getNums(), [1, 1, 2, 3, 5, 8])
|
|
|
|
t.deepEqual(getWords(), ['foo', 'bar'])
|
|
|
|
|
|
|
|
t.is(sumNums([1, 2, 3, 4, 5]), 15)
|
2022-08-11 23:47:13 +09:00
|
|
|
t.deepEqual(getNumArr(), [1, 2])
|
|
|
|
t.deepEqual(getNestedNumArr(), [[[1]], [[1]]])
|
2021-09-23 02:29:09 +09:00
|
|
|
})
|
|
|
|
|
2021-12-19 00:16:09 +09:00
|
|
|
test('map', (t) => {
|
|
|
|
t.deepEqual(getMapping(), { a: 101, b: 102 })
|
|
|
|
t.is(sumMapping({ a: 101, b: 102 }), 203)
|
2024-02-15 16:43:21 +09:00
|
|
|
t.deepEqual(getBtreeMapping(), { a: 101, b: 102 })
|
|
|
|
t.is(sumBtreeMapping({ a: 101, b: 102 }), 203)
|
|
|
|
t.deepEqual(getIndexMapping(), { a: 101, b: 102 })
|
|
|
|
t.is(sumIndexMapping({ a: 101, b: 102 }), 203)
|
|
|
|
t.deepEqual(indexmapPassthrough({ a: 101, b: 102 }), { a: 101, b: 102 })
|
2021-12-19 00:16:09 +09:00
|
|
|
})
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
test('enum', (t) => {
|
|
|
|
t.deepEqual([Kind.Dog, Kind.Cat, Kind.Duck], [0, 1, 2])
|
|
|
|
t.is(enumToI32(CustomNumEnum.Eight), 8)
|
|
|
|
})
|
|
|
|
|
2024-01-26 00:28:57 +09:00
|
|
|
test('function call', async (t) => {
|
|
|
|
t.is(
|
|
|
|
call0(() => 42),
|
|
|
|
42,
|
|
|
|
)
|
|
|
|
t.is(
|
|
|
|
call1((a) => a + 10, 42),
|
|
|
|
52,
|
|
|
|
)
|
|
|
|
t.is(
|
|
|
|
call2((a, b) => a + b, 42, 10),
|
|
|
|
52,
|
|
|
|
)
|
|
|
|
const ctx = new Animal(Kind.Dog, '旺财')
|
2024-02-26 16:46:07 +09:00
|
|
|
apply0(ctx, function (this: Animal) {
|
2024-01-26 00:28:57 +09:00
|
|
|
this.name = '可乐'
|
|
|
|
})
|
|
|
|
t.is(ctx.name, '可乐')
|
|
|
|
const ctx2 = new Animal(Kind.Dog, '旺财')
|
|
|
|
apply1(
|
|
|
|
ctx2,
|
2024-02-26 16:46:07 +09:00
|
|
|
function (this: Animal, name: string) {
|
2024-01-26 00:28:57 +09:00
|
|
|
this.name = name
|
|
|
|
},
|
|
|
|
'可乐',
|
|
|
|
)
|
|
|
|
t.is(ctx2.name, '可乐')
|
|
|
|
t.is(
|
|
|
|
callFunction(() => 42),
|
|
|
|
42,
|
|
|
|
)
|
|
|
|
t.is(
|
|
|
|
callFunctionWithArg((a, b) => a + b, 42, 10),
|
|
|
|
52,
|
|
|
|
)
|
|
|
|
const ctx3 = new Animal(Kind.Dog, '旺财')
|
|
|
|
callFunctionWithArgAndCtx(
|
|
|
|
ctx3,
|
2024-02-26 16:46:07 +09:00
|
|
|
function (this: Animal, name: string) {
|
2024-01-26 00:28:57 +09:00
|
|
|
this.name = name
|
|
|
|
},
|
|
|
|
'可乐',
|
|
|
|
)
|
|
|
|
t.is(ctx3.name, '可乐')
|
|
|
|
const cbSpy = spy()
|
|
|
|
await createReferenceOnFunction(cbSpy)
|
|
|
|
t.is(cbSpy.callCount, 1)
|
|
|
|
t.is(
|
|
|
|
referenceAsCallback((a, b) => a + b, 42, 10),
|
|
|
|
52,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-08-20 00:36:36 +09:00
|
|
|
test('class', (t) => {
|
2021-09-23 02:29:09 +09:00
|
|
|
const dog = new Animal(Kind.Dog, '旺财')
|
|
|
|
|
|
|
|
t.is(dog.name, '旺财')
|
|
|
|
t.is(dog.kind, Kind.Dog)
|
|
|
|
t.is(dog.whoami(), 'Dog: 旺财')
|
|
|
|
|
2022-08-06 22:54:58 +09:00
|
|
|
t.notThrows(() => {
|
|
|
|
const rawMethod = dog.whoami
|
|
|
|
dog.whoami = function (...args) {
|
|
|
|
return rawMethod.apply(this, args)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
dog.name = '可乐'
|
|
|
|
t.is(dog.name, '可乐')
|
2021-12-23 15:07:35 +09:00
|
|
|
t.deepEqual(dog.returnOtherClass(), new Dog('Doge'))
|
2021-12-24 19:46:10 +09:00
|
|
|
t.deepEqual(dog.returnOtherClassWithCustomConstructor(), new Bird('parrot'))
|
2022-05-22 14:43:11 +09:00
|
|
|
t.is(
|
|
|
|
dog.overrideIndividualArgOnMethod('Jafar', { n: 'Iago' }).name,
|
|
|
|
'Jafar-Iago',
|
|
|
|
)
|
2021-12-24 23:56:16 +09:00
|
|
|
t.is(dog.returnOtherClassWithCustomConstructor().getCount(), 1234)
|
2022-05-13 13:55:54 +09:00
|
|
|
t.is(dog.type, Kind.Dog)
|
|
|
|
dog.type = Kind.Cat
|
|
|
|
t.is(dog.type, Kind.Cat)
|
2021-12-25 18:35:47 +09:00
|
|
|
const assets = new Assets()
|
|
|
|
t.is(assets.get(1)?.filePath, 1)
|
2022-07-06 00:09:40 +09:00
|
|
|
const turtle = NinjaTurtle.newRaph()
|
|
|
|
t.is(turtle.returnThis(), turtle)
|
2022-08-17 16:11:32 +09:00
|
|
|
t.is(NinjaTurtle.isInstanceOf(turtle), true)
|
2022-08-17 18:49:22 +09:00
|
|
|
// Inject this to function
|
|
|
|
const width = new Width(1)
|
|
|
|
t.is(plusOne.call(width), 2)
|
|
|
|
t.throws(() => {
|
|
|
|
// @ts-expect-error
|
|
|
|
plusOne.call('')
|
|
|
|
})
|
2023-12-27 13:49:10 +09:00
|
|
|
|
|
|
|
t.notThrows(() => {
|
|
|
|
new CatchOnConstructor()
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!process.env.TEST_ZIG_CROSS) {
|
|
|
|
t.throws(
|
|
|
|
() => {
|
|
|
|
new CatchOnConstructor2()
|
|
|
|
},
|
|
|
|
(() =>
|
|
|
|
process.env.WASI_TEST
|
|
|
|
? undefined
|
|
|
|
: {
|
|
|
|
message: 'CatchOnConstructor2 panic',
|
|
|
|
})(),
|
|
|
|
)
|
|
|
|
}
|
2021-09-23 02:29:09 +09:00
|
|
|
})
|
|
|
|
|
2022-11-22 01:17:19 +09:00
|
|
|
test('async self in class', async (t) => {
|
|
|
|
const b = new Bird('foo')
|
|
|
|
t.is(await b.getNameAsync(), 'foo')
|
|
|
|
})
|
|
|
|
|
2021-11-05 19:31:36 +09:00
|
|
|
test('class factory', (t) => {
|
|
|
|
const duck = ClassWithFactory.withName('Default')
|
|
|
|
t.is(duck.name, 'Default')
|
|
|
|
|
2021-11-26 00:42:40 +09:00
|
|
|
const ret = duck.setName('D')
|
|
|
|
t.is(ret.name, 'D')
|
|
|
|
t.is(ret, duck)
|
|
|
|
|
2021-11-05 19:31:36 +09:00
|
|
|
duck.name = '周黑鸭'
|
|
|
|
t.is(duck.name, '周黑鸭')
|
|
|
|
|
|
|
|
const doge = Animal.withKind(Kind.Dog)
|
|
|
|
t.is(doge.name, 'Default')
|
|
|
|
|
|
|
|
doge.name = '旺财'
|
|
|
|
t.is(doge.name, '旺财')
|
2021-12-08 18:59:30 +09:00
|
|
|
|
|
|
|
const error = t.throws(() => new ClassWithFactory())
|
|
|
|
t.true(
|
2023-09-20 17:18:01 +09:00
|
|
|
error?.message.startsWith(
|
2021-12-08 18:59:30 +09:00
|
|
|
'Class contains no `constructor`, can not new it!',
|
|
|
|
),
|
|
|
|
)
|
2021-11-05 19:31:36 +09:00
|
|
|
})
|
|
|
|
|
2023-11-06 11:58:23 +09:00
|
|
|
test('async class factory', async (t) => {
|
|
|
|
const instance = await ClassWithFactory.with4Name('foo')
|
|
|
|
t.is(instance.name, 'foo-4')
|
|
|
|
const instance2 = await ClassWithFactory.with4NameResult('foo')
|
|
|
|
t.is(instance2.name, 'foo-4')
|
|
|
|
})
|
|
|
|
|
2021-11-06 22:48:18 +09:00
|
|
|
test('class constructor return Result', (t) => {
|
|
|
|
const c = new Context()
|
|
|
|
t.is(c.method(), 'not empty')
|
|
|
|
})
|
|
|
|
|
2022-07-06 20:08:34 +09:00
|
|
|
test('class default field is TypedArray', (t) => {
|
|
|
|
const c = new Context()
|
|
|
|
t.deepEqual(c.buffer, new Uint8Array([0, 1, 2, 3]))
|
|
|
|
const fixture = new Uint8Array([0, 1, 2, 3, 4, 5, 6])
|
|
|
|
const c2 = Context.withBuffer(fixture)
|
|
|
|
t.is(c2.buffer, fixture)
|
|
|
|
})
|
|
|
|
|
2021-11-06 22:48:18 +09:00
|
|
|
test('class Factory return Result', (t) => {
|
|
|
|
const c = Context.withData('not empty')
|
|
|
|
t.is(c.method(), 'not empty')
|
|
|
|
})
|
|
|
|
|
2022-06-04 02:07:39 +09:00
|
|
|
test('class in object field', (t) => {
|
|
|
|
const obj = createObjectWithClassField()
|
|
|
|
t.is(obj.bird.name, 'Carolyn')
|
|
|
|
t.is(receiveObjectWithClassField(obj), obj.bird)
|
|
|
|
})
|
|
|
|
|
2022-08-17 14:24:40 +09:00
|
|
|
test('custom finalize class', (t) => {
|
|
|
|
t.notThrows(() => new CustomFinalize(200, 200))
|
|
|
|
})
|
|
|
|
|
2022-03-21 17:36:06 +09:00
|
|
|
test('should be able to create object reference and shared reference', (t) => {
|
|
|
|
const repo = new JsRepo('.')
|
|
|
|
t.is(repo.remote().name(), 'origin')
|
2024-02-22 23:37:50 +09:00
|
|
|
t.is(new JsRemote(repo).name(), 'origin')
|
2022-03-21 17:36:06 +09:00
|
|
|
})
|
|
|
|
|
2022-04-26 19:06:52 +09:00
|
|
|
test('should be able to into_reference', (t) => {
|
|
|
|
const rules = ['body: { color: red }', 'div: { color: blue }']
|
2022-05-08 10:48:10 +09:00
|
|
|
const sheet = new CssStyleSheet('test.css', rules)
|
2022-04-26 19:06:52 +09:00
|
|
|
t.is(sheet.rules, sheet.rules)
|
|
|
|
t.deepEqual(sheet.rules.getRules(), rules)
|
2022-05-08 10:48:10 +09:00
|
|
|
t.is(sheet.rules.parentStyleSheet, sheet)
|
|
|
|
t.is(sheet.rules.name, 'test.css')
|
2022-05-03 09:27:18 +09:00
|
|
|
const anotherStyleSheet = sheet.anotherCssStyleSheet()
|
|
|
|
t.is(anotherStyleSheet.rules, sheet.rules)
|
2022-04-26 19:06:52 +09:00
|
|
|
})
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
test('callback', (t) => {
|
|
|
|
getCwd((cwd) => {
|
2023-11-02 13:57:11 +09:00
|
|
|
t.is(cwd, process.env.WASI_TEST ? '/' : process.cwd())
|
2021-09-23 02:29:09 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
t.throws(
|
|
|
|
// @ts-expect-error
|
|
|
|
() => getCwd(),
|
2022-01-04 12:04:45 +09:00
|
|
|
void 0,
|
2021-09-23 02:29:09 +09:00
|
|
|
'Expect value to be Function, but received Undefined',
|
|
|
|
)
|
2021-09-24 10:15:48 +09:00
|
|
|
|
|
|
|
readFile((err, content) => {
|
2021-09-24 10:46:27 +09:00
|
|
|
t.is(err, undefined)
|
2021-09-24 10:15:48 +09:00
|
|
|
t.is(content, 'hello world')
|
|
|
|
})
|
2022-09-14 20:30:43 +09:00
|
|
|
|
|
|
|
captureErrorInCallback(
|
|
|
|
() => {
|
|
|
|
throw new Error('Testing')
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
t.is((err as Error).message, 'Testing')
|
|
|
|
},
|
|
|
|
)
|
2021-09-23 02:29:09 +09:00
|
|
|
})
|
|
|
|
|
2022-01-23 19:17:00 +09:00
|
|
|
test('return function', (t) => {
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
returnJsFunction()((err: Error | undefined, content: string) => {
|
|
|
|
t.is(err, undefined)
|
|
|
|
t.is(content, 'hello world')
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-11-02 13:57:11 +09:00
|
|
|
Napi4Test('callback function return Promise', async (t) => {
|
2022-04-14 01:02:06 +09:00
|
|
|
const cbSpy = spy()
|
|
|
|
await callbackReturnPromise<string>(() => '1', spy)
|
|
|
|
t.is(cbSpy.callCount, 0)
|
|
|
|
await callbackReturnPromise(
|
|
|
|
() => Promise.resolve('42'),
|
|
|
|
(err, res) => {
|
|
|
|
t.is(err, null)
|
|
|
|
cbSpy(res)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
t.is(cbSpy.callCount, 1)
|
|
|
|
t.deepEqual(cbSpy.args, [['42']])
|
|
|
|
})
|
|
|
|
|
2023-11-02 13:57:11 +09:00
|
|
|
Napi4Test('callback function return Promise and spawn', async (t) => {
|
2023-05-16 12:05:37 +09:00
|
|
|
const finalReturn = await callbackReturnPromiseAndSpawn((input) =>
|
|
|
|
Promise.resolve(`${input} world`),
|
|
|
|
)
|
|
|
|
t.is(finalReturn, 'Hello world 😼')
|
|
|
|
})
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
test('object', (t) => {
|
|
|
|
t.deepEqual(listObjKeys({ name: 'John Doe', age: 20 }), ['name', 'age'])
|
|
|
|
t.deepEqual(createObj(), { test: 1 })
|
2024-02-28 15:29:02 +09:00
|
|
|
t.throws(
|
|
|
|
() =>
|
|
|
|
objectGetNamedPropertyShouldPerformTypecheck({
|
|
|
|
// @ts-expect-error
|
|
|
|
foo: '2',
|
|
|
|
bar: '3',
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
message: `Object property 'foo' type mismatch. Expect value to be Number, but received String`,
|
|
|
|
code: 'InvalidArg',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
t.throws(
|
|
|
|
() =>
|
|
|
|
objectGetNamedPropertyShouldPerformTypecheck({
|
|
|
|
foo: 2,
|
|
|
|
// @ts-expect-error
|
|
|
|
bar: 3,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
message: `Object property 'bar' type mismatch. Expect value to be String, but received Number`,
|
|
|
|
code: 'InvalidArg',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
t.notThrows(() =>
|
|
|
|
objectGetNamedPropertyShouldPerformTypecheck({
|
|
|
|
foo: 2,
|
|
|
|
bar: '3',
|
|
|
|
}),
|
|
|
|
)
|
2024-03-10 22:22:46 +09:00
|
|
|
t.deepEqual(returnObjectOnlyToJs(), {
|
|
|
|
name: 42,
|
|
|
|
dependencies: {
|
|
|
|
'@napi-rs/cli': '^3.0.0',
|
|
|
|
rollup: '^4.0.0',
|
|
|
|
},
|
|
|
|
})
|
2024-04-10 17:52:23 +09:00
|
|
|
t.throws(
|
|
|
|
() =>
|
|
|
|
receiveAllOptionalObject({
|
|
|
|
// @ts-expect-error
|
|
|
|
name: 1,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
code: 'StringExpected',
|
|
|
|
message:
|
|
|
|
'Failed to convert JavaScript value `Number 1 ` into rust type `String` on AllOptionalObject.name',
|
|
|
|
},
|
|
|
|
)
|
2021-09-23 02:29:09 +09:00
|
|
|
})
|
|
|
|
|
2022-01-12 16:19:05 +09:00
|
|
|
test('get str from object', (t) => {
|
|
|
|
t.notThrows(() => getStrFromObject())
|
|
|
|
})
|
|
|
|
|
2022-02-06 17:25:32 +09:00
|
|
|
test('create object from Property', (t) => {
|
|
|
|
const obj = createObjWithProperty()
|
|
|
|
t.true(obj.value instanceof ArrayBuffer)
|
|
|
|
t.is(obj.getter, 42)
|
|
|
|
})
|
|
|
|
|
2021-11-15 19:56:06 +09:00
|
|
|
test('global', (t) => {
|
|
|
|
t.is(getGlobal(), global)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('get undefined', (t) => {
|
|
|
|
for (const _ of Array.from({ length: 100 })) {
|
|
|
|
t.is(getUndefined(), undefined)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
test('get null', (t) => {
|
|
|
|
for (const _ of Array.from({ length: 100 })) {
|
|
|
|
t.is(getNull(), null)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-12-18 14:36:45 +09:00
|
|
|
test('return Null', (t) => {
|
|
|
|
t.is(returnNull(), null)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('return Undefined', (t) => {
|
|
|
|
t.is(returnUndefined(), undefined)
|
|
|
|
})
|
|
|
|
|
2021-11-16 00:09:44 +09:00
|
|
|
test('pass symbol in', (t) => {
|
|
|
|
const sym = Symbol('test')
|
|
|
|
const obj = setSymbolInObj(sym)
|
fix: prevent crashing when napi_register_module_v1 is called twice (#1554)
* fix: prevent crashing when napi_register_module_v1 is called twice
Currently napi-rs addons can lead to the Node.js process aborting with
the following error when initialising the addon on Windows:
```
c:\ws\src\cleanup_queue-inl.h:32: Assertion `(insertion_info.second)
== (true)' failed.
```
This happens because `napi_add_env_cleanup_hook` must not be called
with the same arguments multiple times unless the previously scheduled
cleanup hook with the same arguments was already executed. However,
the cleanup hook added by `napi_register_module_v1` in napi-rs on
Windows was always created with `ptr::null_mut()` as an argument.
One case where this causes a problem is when using the addon from
multiple contexts (e.g. Node.js worker threads) at the same
time. However, Node.js doesn't provide any guarantees that the N-API
addon initialisation code will run only once even per thread and
context. In fact, it's totally valid to run `process.dlopen()`
multiple times from JavaScript land in Node.js, and this will lead to
the initialisation code being run multiple times as different
`exports` objects may need to be populated. This may happen in
numerous cases, e.g.:
- When it's not possible or not desirable to use `require()` and users
must resort to using `process.dlopen()` (one use case is passing
non-default flags to `dlopen(3)`, another is ES modules). Caching
the results of `process.dlopen()` to avoid running it more than once
may not always be possible reliably in all cases (for example,
because of Jest sandbox).
- When the `require` cache is cleared.
- On Windows: `require("./addon.node")` and then
`require(path.toNamespacedPath("./addon.node"))`.
Another issue is fixed inside `napi::tokio_runtime::drop_runtime`:
there's no need to call `napi_remove_env_cleanup_hook` (it's only
useful to cancel the hooks that haven't been executed yet). Null
pointer retrieved from `arg` was being passed as the `env` argument of
that function, so it didn't do anything and just returned
`napi_invalid_arg`.
This patch makes `napi_register_module_v1` use a counter as the
cleanup hook argument, so that the value is always different. An
alternative might have been to use a higher-level abstraction around
`sys::napi_env_cleanup_hook` that would take ownership of a boxed
closure, if there is something like this in the API already. Another
alternative could have been to heap-allocate a value so that we would
have a unique valid memory address.
The patch also contains a minor code cleanup related to
`RT_REFERENCE_COUNT` along the way: the counter is encapsulated inside
its module and `ensure_runtime` takes care of incrementing it, and
less strict memory ordering is now used as there's no need for
`SeqCst` here. If desired, it can be further optimised to
`Ordering::Release` and a separate acquire fence inside the if
statement in `drop_runtime`, as `AcqRel` for every decrement is also a
bit stricter than necessary (although simpler). These changes are not
necessary to fix the issue and can be extracted to a separate patch.
At first it was tempting to use the loaded value of
`RT_REFERENCE_COUNT` as the argument for the cleanup hook but it would
have been wrong: a simple counterexample is the following sequence:
1. init in the first context (queue: 0)
2. init in the second context (queue: 0, 1)
3. destroy the first context (queue: 1)
4. init in the third context (queue: 1, 1)
* test(napi): unload test was excluded unexpected
---------
Co-authored-by: LongYinan <lynweklm@gmail.com>
2023-04-09 00:08:48 +09:00
|
|
|
// @ts-expect-error
|
2021-11-16 00:09:44 +09:00
|
|
|
t.is(obj[sym], 'a symbol')
|
|
|
|
})
|
|
|
|
|
|
|
|
test('create symbol', (t) => {
|
|
|
|
t.is(createSymbol().toString(), 'Symbol(a symbol)')
|
|
|
|
})
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
test('Option', (t) => {
|
2021-09-24 10:46:27 +09:00
|
|
|
t.is(mapOption(null), null)
|
2021-09-23 02:29:09 +09:00
|
|
|
t.is(mapOption(3), 4)
|
|
|
|
})
|
2021-09-24 10:46:27 +09:00
|
|
|
|
|
|
|
test('Result', (t) => {
|
2022-01-04 12:04:45 +09:00
|
|
|
t.throws(() => throwError(), void 0, 'Manual Error')
|
2022-08-20 00:36:36 +09:00
|
|
|
if (!process.env.SKIP_UNWIND_TEST) {
|
|
|
|
t.throws(() => panic(), void 0, `Don't panic`)
|
|
|
|
}
|
2021-09-24 10:46:27 +09:00
|
|
|
})
|
2021-09-28 01:01:19 +09:00
|
|
|
|
2023-07-15 13:07:14 +09:00
|
|
|
test('Async error with stack trace', async (t) => {
|
|
|
|
const err = await t.throwsAsync(() => throwAsyncError())
|
|
|
|
t.not(err?.stack, undefined)
|
|
|
|
t.deepEqual(err!.message, 'Async Error')
|
2023-11-02 13:57:11 +09:00
|
|
|
if (!process.env.WASI_TEST) {
|
|
|
|
t.regex(err!.stack!, /.+at .+values\.spec\.ts:\d+:\d+.+/gm)
|
|
|
|
}
|
2023-07-15 13:07:14 +09:00
|
|
|
})
|
|
|
|
|
2023-02-10 00:18:57 +09:00
|
|
|
test('custom status code in Error', (t) => {
|
|
|
|
t.throws(() => customStatusCode(), {
|
|
|
|
code: 'Panic',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-26 18:26:14 +09:00
|
|
|
test('function ts type override', (t) => {
|
fix: prevent crashing when napi_register_module_v1 is called twice (#1554)
* fix: prevent crashing when napi_register_module_v1 is called twice
Currently napi-rs addons can lead to the Node.js process aborting with
the following error when initialising the addon on Windows:
```
c:\ws\src\cleanup_queue-inl.h:32: Assertion `(insertion_info.second)
== (true)' failed.
```
This happens because `napi_add_env_cleanup_hook` must not be called
with the same arguments multiple times unless the previously scheduled
cleanup hook with the same arguments was already executed. However,
the cleanup hook added by `napi_register_module_v1` in napi-rs on
Windows was always created with `ptr::null_mut()` as an argument.
One case where this causes a problem is when using the addon from
multiple contexts (e.g. Node.js worker threads) at the same
time. However, Node.js doesn't provide any guarantees that the N-API
addon initialisation code will run only once even per thread and
context. In fact, it's totally valid to run `process.dlopen()`
multiple times from JavaScript land in Node.js, and this will lead to
the initialisation code being run multiple times as different
`exports` objects may need to be populated. This may happen in
numerous cases, e.g.:
- When it's not possible or not desirable to use `require()` and users
must resort to using `process.dlopen()` (one use case is passing
non-default flags to `dlopen(3)`, another is ES modules). Caching
the results of `process.dlopen()` to avoid running it more than once
may not always be possible reliably in all cases (for example,
because of Jest sandbox).
- When the `require` cache is cleared.
- On Windows: `require("./addon.node")` and then
`require(path.toNamespacedPath("./addon.node"))`.
Another issue is fixed inside `napi::tokio_runtime::drop_runtime`:
there's no need to call `napi_remove_env_cleanup_hook` (it's only
useful to cancel the hooks that haven't been executed yet). Null
pointer retrieved from `arg` was being passed as the `env` argument of
that function, so it didn't do anything and just returned
`napi_invalid_arg`.
This patch makes `napi_register_module_v1` use a counter as the
cleanup hook argument, so that the value is always different. An
alternative might have been to use a higher-level abstraction around
`sys::napi_env_cleanup_hook` that would take ownership of a boxed
closure, if there is something like this in the API already. Another
alternative could have been to heap-allocate a value so that we would
have a unique valid memory address.
The patch also contains a minor code cleanup related to
`RT_REFERENCE_COUNT` along the way: the counter is encapsulated inside
its module and `ensure_runtime` takes care of incrementing it, and
less strict memory ordering is now used as there's no need for
`SeqCst` here. If desired, it can be further optimised to
`Ordering::Release` and a separate acquire fence inside the if
statement in `drop_runtime`, as `AcqRel` for every decrement is also a
bit stricter than necessary (although simpler). These changes are not
necessary to fix the issue and can be extracted to a separate patch.
At first it was tempting to use the loaded value of
`RT_REFERENCE_COUNT` as the argument for the cleanup hook but it would
have been wrong: a simple counterexample is the following sequence:
1. init in the first context (queue: 0)
2. init in the second context (queue: 0, 1)
3. destroy the first context (queue: 1)
4. init in the third context (queue: 1, 1)
* test(napi): unload test was excluded unexpected
---------
Co-authored-by: LongYinan <lynweklm@gmail.com>
2023-04-09 00:08:48 +09:00
|
|
|
// @ts-expect-error
|
2021-11-26 18:26:14 +09:00
|
|
|
t.deepEqual(tsRename({ foo: 1, bar: 2, baz: 2 }), ['foo', 'bar', 'baz'])
|
|
|
|
})
|
|
|
|
|
2022-05-22 14:43:11 +09:00
|
|
|
test('function individual ts arg type override', (t) => {
|
|
|
|
t.is(
|
|
|
|
overrideIndividualArgOnFunction('someStr', () => 'anotherStr', 42),
|
|
|
|
'oia: someStr-42-anotherStr',
|
|
|
|
)
|
|
|
|
t.deepEqual(
|
|
|
|
overrideIndividualArgOnFunctionWithCbArg(
|
|
|
|
(town, opt) => `im: ${town}-${opt}`,
|
|
|
|
89,
|
|
|
|
),
|
|
|
|
'im: World(89)-null',
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-12-02 16:47:59 +09:00
|
|
|
test('option object', (t) => {
|
|
|
|
t.notThrows(() => receiveAllOptionalObject())
|
|
|
|
t.notThrows(() => receiveAllOptionalObject({}))
|
|
|
|
})
|
|
|
|
|
2022-01-06 16:56:14 +09:00
|
|
|
test('should throw if object type is not matched', (t) => {
|
|
|
|
// @ts-expect-error
|
|
|
|
const err1 = t.throws(() => receiveStrictObject({ name: 1 }))
|
2023-02-06 01:52:59 +09:00
|
|
|
t.is(
|
2023-09-20 17:18:01 +09:00
|
|
|
err1?.message,
|
2024-04-10 17:52:23 +09:00
|
|
|
'Failed to convert JavaScript value `Number 1 ` into rust type `String` on StrictObject.name',
|
2023-02-06 01:52:59 +09:00
|
|
|
)
|
2022-01-06 16:56:14 +09:00
|
|
|
// @ts-expect-error
|
|
|
|
const err2 = t.throws(() => receiveStrictObject({ bar: 1 }))
|
|
|
|
t.is(err2!.message, 'Missing field `name`')
|
|
|
|
})
|
|
|
|
|
2021-12-02 17:25:20 +09:00
|
|
|
test('aliased rust struct and enum', (t) => {
|
2023-09-20 17:18:01 +09:00
|
|
|
const a = ALIAS.A
|
2021-12-02 17:25:20 +09:00
|
|
|
const b: AliasedStruct = {
|
|
|
|
a,
|
|
|
|
b: 1,
|
|
|
|
}
|
|
|
|
t.notThrows(() => fnReceivedAliased(b, ALIAS.B))
|
|
|
|
})
|
|
|
|
|
2021-09-28 01:01:19 +09:00
|
|
|
test('serde-json', (t) => {
|
2023-11-02 13:57:11 +09:00
|
|
|
if (process.env.WASI_TEST) {
|
|
|
|
t.pass()
|
|
|
|
return
|
|
|
|
}
|
2021-09-28 01:01:19 +09:00
|
|
|
const packageJson = readPackageJson()
|
2023-04-06 12:04:53 +09:00
|
|
|
t.is(packageJson.name, '@examples/napi')
|
2021-09-28 01:01:19 +09:00
|
|
|
t.is(packageJson.version, '0.0.0')
|
|
|
|
t.snapshot(Object.keys(packageJson.devDependencies!).sort())
|
|
|
|
|
2023-04-06 12:04:53 +09:00
|
|
|
t.is(getPackageJsonName(packageJson), '@examples/napi')
|
2021-09-28 01:01:19 +09:00
|
|
|
})
|
2021-10-08 22:13:27 +09:00
|
|
|
|
2022-02-06 12:58:17 +09:00
|
|
|
test('serde-roundtrip', (t) => {
|
|
|
|
t.is(testSerdeRoundtrip(1), 1)
|
|
|
|
t.is(testSerdeRoundtrip(1.2), 1.2)
|
|
|
|
t.is(testSerdeRoundtrip(-1), -1)
|
|
|
|
|
|
|
|
t.deepEqual(testSerdeRoundtrip([1, 1.2, -1]), [1, 1.2, -1])
|
|
|
|
t.deepEqual(testSerdeRoundtrip({ a: 1, b: 1.2, c: -1 }), {
|
|
|
|
a: 1,
|
|
|
|
b: 1.2,
|
|
|
|
c: -1,
|
|
|
|
})
|
|
|
|
t.throws(() => testSerdeRoundtrip(NaN))
|
|
|
|
|
|
|
|
t.is(testSerdeRoundtrip(null), null)
|
|
|
|
|
|
|
|
let err = t.throws(() => testSerdeRoundtrip(undefined))
|
2023-09-20 17:18:01 +09:00
|
|
|
t.is(err?.message, 'undefined cannot be represented as a serde_json::Value')
|
2022-02-06 12:58:17 +09:00
|
|
|
|
|
|
|
err = t.throws(() => testSerdeRoundtrip(() => {}))
|
|
|
|
t.is(
|
|
|
|
err!.message,
|
|
|
|
'JS functions cannot be represented as a serde_json::Value',
|
|
|
|
)
|
|
|
|
|
|
|
|
err = t.throws(() => testSerdeRoundtrip(Symbol.for('foo')))
|
|
|
|
t.is(err!.message, 'JS symbols cannot be represented as a serde_json::Value')
|
|
|
|
})
|
|
|
|
|
2023-03-23 14:34:34 +09:00
|
|
|
test('serde-large-number-precision', (t) => {
|
|
|
|
t.is(testSerdeBigNumberPrecision('12345').number, 12345)
|
|
|
|
t.is(
|
|
|
|
testSerdeBigNumberPrecision('123456789012345678901234567890').number,
|
|
|
|
1.2345678901234568e29,
|
|
|
|
)
|
|
|
|
t.is(
|
|
|
|
testSerdeBigNumberPrecision('123456789012345678901234567890.123456789')
|
|
|
|
.number,
|
|
|
|
1.2345678901234568e29,
|
|
|
|
)
|
|
|
|
t.is(
|
|
|
|
testSerdeBigNumberPrecision('109775245175819965').number.toString(),
|
|
|
|
'109775245175819965',
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-10-08 22:13:27 +09:00
|
|
|
test('buffer', (t) => {
|
2021-12-18 14:24:16 +09:00
|
|
|
let buf = getBuffer()
|
|
|
|
t.is(buf.toString('utf-8'), 'Hello world')
|
|
|
|
buf = appendBuffer(buf)
|
|
|
|
t.is(buf.toString('utf-8'), 'Hello world!')
|
2022-04-23 17:43:36 +09:00
|
|
|
|
|
|
|
const a = getEmptyBuffer()
|
|
|
|
const b = getEmptyBuffer()
|
|
|
|
t.is(a.toString(), '')
|
|
|
|
t.is(b.toString(), '')
|
2024-01-26 15:00:52 +09:00
|
|
|
|
|
|
|
// @ts-expect-error
|
|
|
|
t.true(Array.isArray(asyncBufferToArray(Buffer.from([1, 2, 3]).buffer)))
|
2021-10-08 22:13:27 +09:00
|
|
|
})
|
2021-10-25 01:00:31 +09:00
|
|
|
|
2024-02-16 00:40:45 +09:00
|
|
|
test('TypedArray', (t) => {
|
|
|
|
t.is(acceptSlice(new Uint8Array([1, 2, 3])), 3n)
|
|
|
|
t.deepEqual(u8ArrayToArray(new Uint8Array([1, 2, 3])), [1, 2, 3])
|
|
|
|
t.deepEqual(i8ArrayToArray(new Int8Array([1, 2, 3])), [1, 2, 3])
|
|
|
|
t.deepEqual(u16ArrayToArray(new Uint16Array([1, 2, 3])), [1, 2, 3])
|
|
|
|
t.deepEqual(i16ArrayToArray(new Int16Array([1, 2, 3])), [1, 2, 3])
|
|
|
|
t.deepEqual(u32ArrayToArray(new Uint32Array([1, 2, 3])), [1, 2, 3])
|
|
|
|
t.deepEqual(i32ArrayToArray(new Int32Array([1, 2, 3])), [1, 2, 3])
|
|
|
|
t.deepEqual(u64ArrayToArray(new BigUint64Array([1n, 2n, 3n])), [1n, 2n, 3n])
|
|
|
|
t.deepEqual(i64ArrayToArray(new BigInt64Array([1n, 2n, 3n])), [1, 2, 3])
|
|
|
|
t.deepEqual(f32ArrayToArray(new Float32Array([1, 2, 3])), [1, 2, 3])
|
|
|
|
t.deepEqual(f64ArrayToArray(new Float64Array([1, 2, 3])), [1, 2, 3])
|
|
|
|
|
|
|
|
const bird = new Bird('Carolyn')
|
|
|
|
|
|
|
|
t.is(bird.acceptSliceMethod(new Uint8Array([1, 2, 3])), 3)
|
2024-02-25 02:00:28 +09:00
|
|
|
|
|
|
|
t.is(acceptUint8ClampedSlice(new Uint8ClampedArray([1, 2, 3])), 3n)
|
|
|
|
t.is(
|
|
|
|
acceptUint8ClampedSliceAndBufferSlice(
|
|
|
|
Buffer.from([1, 2, 3]),
|
|
|
|
new Uint8ClampedArray([1, 2, 3]),
|
|
|
|
),
|
|
|
|
6n,
|
|
|
|
)
|
2024-02-16 00:40:45 +09:00
|
|
|
})
|
|
|
|
|
2022-04-26 16:14:37 +09:00
|
|
|
test('reset empty buffer', (t) => {
|
|
|
|
const empty = getEmptyBuffer()
|
|
|
|
|
|
|
|
const shared = new ArrayBuffer(0)
|
|
|
|
const buffer = Buffer.from(shared)
|
|
|
|
t.notThrows(() => {
|
|
|
|
buffer.set(empty)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-30 19:43:34 +09:00
|
|
|
test('convert typedarray to vec', (t) => {
|
|
|
|
const input = new Uint32Array([1, 2, 3, 4, 5])
|
|
|
|
t.deepEqual(convertU32Array(input), Array.from(input))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('create external TypedArray', (t) => {
|
|
|
|
t.deepEqual(createExternalTypedArray(), new Uint32Array([1, 2, 3, 4, 5]))
|
|
|
|
})
|
|
|
|
|
|
|
|
test('mutate TypedArray', (t) => {
|
2023-11-07 02:46:43 +09:00
|
|
|
if (process.env.WASI_TEST) {
|
2023-11-02 13:57:11 +09:00
|
|
|
t.pass()
|
|
|
|
return
|
|
|
|
}
|
2021-11-30 19:43:34 +09:00
|
|
|
const input = new Float32Array([1, 2, 3, 4, 5])
|
|
|
|
mutateTypedArray(input)
|
|
|
|
t.deepEqual(input, new Float32Array([2.0, 4.0, 6.0, 8.0, 10.0]))
|
|
|
|
})
|
|
|
|
|
2022-02-07 13:24:09 +09:00
|
|
|
test('deref uint8 array', (t) => {
|
|
|
|
t.is(
|
|
|
|
derefUint8Array(new Uint8Array([1, 2]), new Uint8ClampedArray([3, 4])),
|
|
|
|
4,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-10-25 01:00:31 +09:00
|
|
|
test('async', async (t) => {
|
2023-11-02 13:57:11 +09:00
|
|
|
if (process.env.WASI_TEST) {
|
|
|
|
t.pass()
|
|
|
|
return
|
|
|
|
}
|
2021-10-25 01:00:31 +09:00
|
|
|
const bufPromise = readFileAsync(join(__dirname, '../package.json'))
|
|
|
|
await t.notThrowsAsync(bufPromise)
|
|
|
|
const buf = await bufPromise
|
|
|
|
const { name } = JSON.parse(buf.toString())
|
2023-04-06 12:04:53 +09:00
|
|
|
t.is(name, '@examples/napi')
|
2021-10-25 01:00:31 +09:00
|
|
|
|
|
|
|
await t.throwsAsync(() => readFileAsync('some_nonexist_path.file'))
|
2024-04-19 17:00:20 +09:00
|
|
|
})
|
2024-04-18 00:34:47 +09:00
|
|
|
|
2024-04-19 17:00:20 +09:00
|
|
|
test('panic in async fn', async (t) => {
|
2024-04-18 00:34:47 +09:00
|
|
|
if (!process.env.SKIP_UNWIND_TEST) {
|
|
|
|
await t.throwsAsync(() => panicInAsync(), {
|
|
|
|
message: 'panic in async function',
|
|
|
|
})
|
2024-04-19 17:00:20 +09:00
|
|
|
} else {
|
|
|
|
t.pass('no unwind runtime')
|
2024-04-18 00:34:47 +09:00
|
|
|
}
|
2021-10-25 01:00:31 +09:00
|
|
|
})
|
2021-11-02 01:34:19 +09:00
|
|
|
|
2021-11-06 23:19:42 +09:00
|
|
|
test('async move', async (t) => {
|
|
|
|
t.is(await asyncMultiTwo(2), 4)
|
|
|
|
})
|
|
|
|
|
2022-02-28 02:26:48 +09:00
|
|
|
test('buffer passthrough', async (t) => {
|
|
|
|
const fixture = Buffer.from('hello world')
|
|
|
|
const ret = await bufferPassThrough(fixture)
|
|
|
|
t.deepEqual(ret, fixture)
|
|
|
|
})
|
|
|
|
|
2022-07-06 20:08:34 +09:00
|
|
|
test('arraybuffer passthrough', async (t) => {
|
|
|
|
const fixture = new Uint8Array([1, 2, 3, 4, 5])
|
|
|
|
const ret = await arrayBufferPassThrough(fixture)
|
|
|
|
t.deepEqual(ret, fixture)
|
|
|
|
})
|
|
|
|
|
2022-04-06 22:08:00 +09:00
|
|
|
test('async reduce buffer', async (t) => {
|
|
|
|
const input = [1, 2, 3, 4, 5, 6]
|
|
|
|
const fixture = Buffer.from(input)
|
|
|
|
t.is(
|
|
|
|
await asyncReduceBuffer(fixture),
|
|
|
|
input.reduce((acc, cur) => acc + cur),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-11-02 01:34:19 +09:00
|
|
|
test('either', (t) => {
|
|
|
|
t.is(eitherStringOrNumber(2), 2)
|
|
|
|
t.is(eitherStringOrNumber('hello'), 'hello'.length)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('return either', (t) => {
|
|
|
|
t.is(returnEither(2), 2)
|
|
|
|
t.is(returnEither(42), '42')
|
|
|
|
})
|
|
|
|
|
2022-01-06 17:16:43 +09:00
|
|
|
test('receive class reference in either', (t) => {
|
|
|
|
const c = new JsClassForEither()
|
|
|
|
t.is(receiveClassOrNumber(1), 2)
|
|
|
|
t.is(receiveClassOrNumber(c), 100)
|
|
|
|
t.is(receiveMutClassOrNumber(c), 100)
|
|
|
|
})
|
|
|
|
|
2022-07-05 18:00:45 +09:00
|
|
|
test('receive different class', (t) => {
|
2023-11-02 13:57:11 +09:00
|
|
|
// TODO: fix the napi_unwrap error from the emnapi
|
2023-11-07 02:46:43 +09:00
|
|
|
if (process.env.WASI_TEST) {
|
2023-11-02 13:57:11 +09:00
|
|
|
t.pass()
|
|
|
|
return
|
|
|
|
}
|
2022-07-05 18:00:45 +09:00
|
|
|
const a = new JsClassForEither()
|
|
|
|
const b = new AnotherClassForEither()
|
|
|
|
t.is(receiveDifferentClass(a), 42)
|
|
|
|
t.is(receiveDifferentClass(b), 100)
|
|
|
|
})
|
|
|
|
|
2022-05-12 16:11:47 +09:00
|
|
|
test('return either class', (t) => {
|
|
|
|
t.is(returnEitherClass(1), 1)
|
|
|
|
t.true(returnEitherClass(-1) instanceof JsClassForEither)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('either from option', (t) => {
|
|
|
|
t.true(eitherFromOption() instanceof JsClassForEither)
|
|
|
|
})
|
|
|
|
|
2022-08-07 02:16:28 +09:00
|
|
|
test('either from objects', (t) => {
|
|
|
|
t.is(eitherFromObjects({ foo: 1 }), 'A')
|
|
|
|
t.is(eitherFromObjects({ bar: 2 }), 'B')
|
|
|
|
t.is(eitherFromObjects({ baz: 3 }), 'C')
|
|
|
|
})
|
|
|
|
|
2021-11-02 01:34:19 +09:00
|
|
|
test('either3', (t) => {
|
|
|
|
t.is(either3(2), 2)
|
|
|
|
t.is(either3('hello'), 'hello'.length)
|
|
|
|
t.is(either3(true), 1)
|
|
|
|
t.is(either3(false), 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('either4', (t) => {
|
|
|
|
t.is(either4(2), 2)
|
|
|
|
t.is(either4('hello'), 'hello'.length)
|
|
|
|
t.is(either4(true), 1)
|
|
|
|
t.is(either4(false), 0)
|
|
|
|
t.is(either4({ v: 1 }), 1)
|
|
|
|
t.is(either4({ v: 'world' }), 'world'.length)
|
|
|
|
})
|
2021-11-02 21:36:34 +09:00
|
|
|
|
2021-11-21 17:11:15 +09:00
|
|
|
test('external', (t) => {
|
|
|
|
const FX = 42
|
|
|
|
const ext = createExternal(FX)
|
|
|
|
t.is(getExternal(ext), FX)
|
|
|
|
mutateExternal(ext, FX + 1)
|
|
|
|
t.is(getExternal(ext), FX + 1)
|
|
|
|
// @ts-expect-error
|
|
|
|
t.throws(() => getExternal({}))
|
|
|
|
const ext2 = createExternalString('wtf')
|
|
|
|
// @ts-expect-error
|
|
|
|
const e = t.throws(() => getExternal(ext2))
|
2024-03-25 16:11:11 +09:00
|
|
|
t.is(e?.message, '<u32> on `External` is not the type of wrapped object')
|
2021-11-02 21:36:34 +09:00
|
|
|
})
|
|
|
|
|
2024-04-15 18:54:10 +09:00
|
|
|
test('optional external', (t) => {
|
|
|
|
const FX = 42
|
|
|
|
const extEmpty = createOptionalExternal()
|
|
|
|
t.is(getOptionalExternal(extEmpty), null)
|
|
|
|
const ext = createOptionalExternal(FX)
|
|
|
|
t.is(getOptionalExternal(ext), FX)
|
|
|
|
mutateOptionalExternal(ext, FX + 1)
|
|
|
|
t.is(getOptionalExternal(ext), FX + 1)
|
|
|
|
// @ts-expect-error
|
|
|
|
t.throws(() => getOptionalExternal({}))
|
|
|
|
const ext2 = createExternalString('wtf')
|
|
|
|
// @ts-expect-error
|
|
|
|
const e = t.throws(() => getOptionalExternal(ext2))
|
|
|
|
t.is(e?.message, '<u32> on `External` is not the type of wrapped object')
|
|
|
|
})
|
|
|
|
|
2023-01-31 21:36:59 +09:00
|
|
|
test('should be able to run script', async (t) => {
|
|
|
|
t.is(runScript(`1 + 1`), 2)
|
|
|
|
t.is(await runScript(`Promise.resolve(1)`), 1)
|
|
|
|
})
|
|
|
|
|
2023-03-21 19:12:52 +09:00
|
|
|
test('should be able to return object from shared crate', (t) => {
|
|
|
|
t.deepEqual(returnFromSharedCrate(), {
|
|
|
|
value: 42,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-12 18:22:57 +09:00
|
|
|
const AbortSignalTest =
|
|
|
|
typeof AbortController !== 'undefined' ? test : test.skip
|
2021-11-04 19:44:06 +09:00
|
|
|
|
2021-11-21 17:11:15 +09:00
|
|
|
AbortSignalTest('async task without abort controller', async (t) => {
|
|
|
|
t.is(await withoutAbortController(1, 2), 3)
|
|
|
|
})
|
|
|
|
|
2021-11-12 18:22:57 +09:00
|
|
|
AbortSignalTest('async task with abort controller', async (t) => {
|
2021-11-02 21:36:34 +09:00
|
|
|
const ctrl = new AbortController()
|
2021-11-04 19:44:06 +09:00
|
|
|
const promise = withAbortController(1, 2, ctrl.signal)
|
2021-11-02 21:36:34 +09:00
|
|
|
try {
|
|
|
|
ctrl.abort()
|
|
|
|
await promise
|
|
|
|
t.fail('Should throw AbortError')
|
|
|
|
} catch (err: unknown) {
|
|
|
|
t.is((err as Error).message, 'AbortError')
|
|
|
|
}
|
|
|
|
})
|
2021-11-04 19:44:06 +09:00
|
|
|
|
2021-11-12 18:22:57 +09:00
|
|
|
AbortSignalTest('abort resolved task', async (t) => {
|
2021-11-04 19:44:06 +09:00
|
|
|
const ctrl = new AbortController()
|
|
|
|
await withAbortController(1, 2, ctrl.signal).then(() => ctrl.abort())
|
|
|
|
t.pass('should not throw')
|
|
|
|
})
|
2021-11-11 01:33:51 +09:00
|
|
|
|
|
|
|
const BigIntTest = typeof BigInt !== 'undefined' ? test : test.skip
|
|
|
|
|
|
|
|
BigIntTest('BigInt add', (t) => {
|
|
|
|
t.is(bigintAdd(BigInt(1), BigInt(2)), BigInt(3))
|
|
|
|
})
|
|
|
|
|
|
|
|
BigIntTest('create BigInt', (t) => {
|
|
|
|
t.is(createBigInt(), BigInt('-3689348814741910323300'))
|
|
|
|
})
|
|
|
|
|
|
|
|
BigIntTest('create BigInt i64', (t) => {
|
|
|
|
t.is(createBigIntI64(), BigInt(100))
|
|
|
|
})
|
2021-11-12 18:22:57 +09:00
|
|
|
|
2022-02-04 17:03:32 +09:00
|
|
|
BigIntTest('BigInt get_u64', (t) => {
|
|
|
|
t.is(bigintGetU64AsString(BigInt(0)), '0')
|
|
|
|
})
|
|
|
|
|
2021-11-23 20:00:31 +09:00
|
|
|
BigIntTest('js mod test', (t) => {
|
|
|
|
t.is(xxh64Alias(Buffer.from('hello world')), BigInt('1116'))
|
|
|
|
t.is(xxh3.xxh3_64(Buffer.from('hello world')), BigInt('1116'))
|
|
|
|
t.is(xxh3.xxh128(Buffer.from('hello world')), BigInt('1116'))
|
|
|
|
t.is(xxh2.xxh2Plus(1, 2), 3)
|
|
|
|
t.is(xxh2.xxh3Xxh64Alias(Buffer.from('hello world')), BigInt('1116'))
|
|
|
|
t.is(xxh3.ALIGNMENT, 16)
|
|
|
|
const xx3 = new xxh3.Xxh3()
|
|
|
|
xx3.update(Buffer.from('hello world'))
|
|
|
|
t.is(xx3.digest(), BigInt('1116'))
|
|
|
|
})
|
|
|
|
|
2023-01-17 01:05:19 +09:00
|
|
|
BigIntTest('from i128 i64', (t) => {
|
|
|
|
t.is(bigintFromI64(), BigInt('100'))
|
|
|
|
t.is(bigintFromI128(), BigInt('-100'))
|
|
|
|
})
|
|
|
|
|
2024-03-20 22:37:08 +09:00
|
|
|
Napi4Test('call ThreadsafeFunction', (t) => {
|
2021-11-12 18:22:57 +09:00
|
|
|
let i = 0
|
|
|
|
let value = 0
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
callThreadsafeFunction((err, v) => {
|
|
|
|
t.is(err, null)
|
|
|
|
i++
|
|
|
|
value += v
|
|
|
|
if (i === 100) {
|
|
|
|
resolve()
|
|
|
|
t.is(
|
|
|
|
value,
|
2024-03-20 22:37:08 +09:00
|
|
|
Array.from({ length: 100 }, (_, i) => i).reduce((a, b) => a + b),
|
2021-11-12 18:22:57 +09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-03-20 22:37:08 +09:00
|
|
|
Napi4Test('throw error from ThreadsafeFunction', async (t) => {
|
2021-11-12 18:22:57 +09:00
|
|
|
const throwPromise = new Promise((_, reject) => {
|
|
|
|
threadsafeFunctionThrowError(reject)
|
|
|
|
})
|
|
|
|
const err = await t.throwsAsync(throwPromise)
|
2023-09-20 17:18:01 +09:00
|
|
|
t.is(err?.message, 'ThrowFromNative')
|
2021-11-12 18:22:57 +09:00
|
|
|
})
|
2021-11-13 21:51:14 +09:00
|
|
|
|
2024-03-20 22:37:08 +09:00
|
|
|
Napi4Test('ThreadsafeFunction closure capture data', (t) => {
|
2022-08-20 23:40:26 +09:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
threadsafeFunctionClosureCapture(() => {
|
|
|
|
resolve()
|
|
|
|
t.pass()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-14 01:37:46 +09:00
|
|
|
Napi4Test('resolve value from thread safe function fatal mode', async (t) => {
|
2021-11-21 15:49:04 +09:00
|
|
|
const tsfnFatalMode = new Promise<boolean>((resolve) => {
|
|
|
|
threadsafeFunctionFatalMode(resolve)
|
|
|
|
})
|
|
|
|
t.true(await tsfnFatalMode)
|
|
|
|
})
|
|
|
|
|
2021-12-14 01:37:46 +09:00
|
|
|
Napi4Test('throw error from thread safe function fatal mode', (t) => {
|
2023-09-20 17:18:01 +09:00
|
|
|
const p = exec('node ./tsfn-error.cjs', {
|
2021-12-14 01:37:46 +09:00
|
|
|
cwd: __dirname,
|
|
|
|
})
|
|
|
|
let stderr = Buffer.from([])
|
|
|
|
p.stderr?.on('data', (data) => {
|
|
|
|
stderr = Buffer.concat([stderr, Buffer.from(data)])
|
|
|
|
})
|
|
|
|
return new Promise<void>((resolve) => {
|
|
|
|
p.on('exit', (code) => {
|
|
|
|
t.is(code, 1)
|
2023-11-02 13:57:11 +09:00
|
|
|
const stderrMsg = stderr.toString('utf8')
|
|
|
|
console.info(stderrMsg)
|
2024-03-20 22:37:08 +09:00
|
|
|
t.true(stderrMsg.includes(`Error: Failed to convert JavaScript value`))
|
2021-12-14 01:37:46 +09:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-13 21:51:14 +09:00
|
|
|
Napi4Test('await Promise in rust', async (t) => {
|
|
|
|
const fx = 20
|
|
|
|
const result = await asyncPlus100(
|
|
|
|
new Promise((resolve) => {
|
|
|
|
setTimeout(() => resolve(fx), 50)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
t.is(result, fx + 100)
|
|
|
|
})
|
|
|
|
|
|
|
|
Napi4Test('Promise should reject raw error in rust', async (t) => {
|
|
|
|
const fxError = new Error('What is Happy Planet')
|
|
|
|
const err = await t.throwsAsync(() => asyncPlus100(Promise.reject(fxError)))
|
|
|
|
t.is(err, fxError)
|
|
|
|
})
|
2022-02-06 21:42:48 +09:00
|
|
|
|
2023-01-11 19:54:45 +09:00
|
|
|
Napi4Test('call ThreadsafeFunction with callback', async (t) => {
|
|
|
|
await t.notThrowsAsync(
|
|
|
|
() =>
|
|
|
|
new Promise<void>((resolve) => {
|
|
|
|
tsfnCallWithCallback(() => {
|
|
|
|
resolve()
|
|
|
|
return 'ReturnFromJavaScriptRawCallback'
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
Napi4Test('async call ThreadsafeFunction', async (t) => {
|
|
|
|
await t.notThrowsAsync(() =>
|
2024-03-20 22:37:08 +09:00
|
|
|
tsfnAsyncCall((arg1, arg2, arg3) => {
|
2023-01-11 19:54:45 +09:00
|
|
|
t.is(arg1, 0)
|
|
|
|
t.is(arg2, 1)
|
|
|
|
t.is(arg3, 2)
|
|
|
|
return 'ReturnFromJavaScriptRawCallback'
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-04-10 18:02:13 +09:00
|
|
|
test('Throw from ThreadsafeFunction JavaScript callback', async (t) => {
|
|
|
|
const errMsg = 'ThrowFromJavaScriptRawCallback'
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
|
|
|
tsfnThrowFromJs(() => {
|
|
|
|
throw new Error(errMsg)
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
message: errMsg,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-01-24 15:25:05 +09:00
|
|
|
Napi4Test('accept ThreadsafeFunction', async (t) => {
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
acceptThreadsafeFunction((err, value) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
t.is(value, 1)
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Napi4Test('accept ThreadsafeFunction Fatal', async (t) => {
|
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
acceptThreadsafeFunctionFatal((value) => {
|
|
|
|
t.is(value, 1)
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-02-08 23:30:12 +09:00
|
|
|
Napi4Test('accept ThreadsafeFunction tuple args', async (t) => {
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
|
|
acceptThreadsafeFunctionTupleArgs((err, num, bool, str) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
t.is(num, 1)
|
|
|
|
t.is(bool, false)
|
|
|
|
t.is(str, 'NAPI-RS')
|
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-11-02 13:57:11 +09:00
|
|
|
Napi4Test('threadsafe function return Promise and await in Rust', async (t) => {
|
2023-03-14 16:32:17 +09:00
|
|
|
const value = await tsfnReturnPromise((err, value) => {
|
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
return Promise.resolve(value + 2)
|
|
|
|
})
|
|
|
|
t.is(value, 5)
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
|
|
|
tsfnReturnPromiseTimeout((err, value) => {
|
|
|
|
if (err) {
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve(value + 2)
|
|
|
|
}, 300)
|
|
|
|
})
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
message: 'Timeout',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
// trigger Promise.then in Rust after `Promise` is dropped
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 400))
|
|
|
|
})
|
|
|
|
|
2023-01-24 15:51:16 +09:00
|
|
|
Napi4Test('object only from js', (t) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
receiveObjectOnlyFromJs({
|
|
|
|
count: 100,
|
|
|
|
callback: (err: Error | null, count: number) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
t.is(count, 100)
|
|
|
|
resolve()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-03-20 22:37:08 +09:00
|
|
|
Napi4Test('build ThreadsafeFunction from Function', (t) => {
|
|
|
|
const subject = new Subject<void>()
|
|
|
|
const fn = (a: number, b: number) => {
|
|
|
|
t.is(a, 1)
|
|
|
|
t.is(b, 2)
|
|
|
|
subject.next()
|
|
|
|
return a * b
|
|
|
|
}
|
|
|
|
|
|
|
|
buildThreadsafeFunctionFromFunction(fn)
|
|
|
|
|
|
|
|
return subject.pipe(take(3))
|
|
|
|
})
|
|
|
|
|
2023-11-02 13:57:11 +09:00
|
|
|
Napi4Test('promise in either', async (t) => {
|
2023-01-24 20:07:33 +09:00
|
|
|
t.is(await promiseInEither(1), false)
|
|
|
|
t.is(await promiseInEither(20), true)
|
|
|
|
t.is(await promiseInEither(Promise.resolve(1)), false)
|
|
|
|
t.is(await promiseInEither(Promise.resolve(20)), true)
|
|
|
|
// @ts-expect-error
|
|
|
|
t.throws(() => promiseInEither('1'))
|
|
|
|
})
|
|
|
|
|
2022-02-06 21:42:48 +09:00
|
|
|
const Napi5Test = Number(process.versions.napi) >= 5 ? test : test.skip
|
|
|
|
|
|
|
|
Napi5Test('Date test', (t) => {
|
|
|
|
const fixture = new Date('2016-12-24')
|
|
|
|
t.is(dateToNumber(fixture), fixture.valueOf())
|
|
|
|
})
|
2022-02-10 04:50:46 +09:00
|
|
|
|
|
|
|
Napi5Test('Date to chrono test', (t) => {
|
|
|
|
const fixture = new Date('2022-02-09T19:31:55.396Z')
|
2024-04-30 17:12:30 +09:00
|
|
|
t.is(chronoUtcDateToMillis(fixture), fixture.getTime())
|
|
|
|
t.is(chronoLocalDateToMillis(fixture), fixture.getTime())
|
|
|
|
t.is(chronoDateWithTimezoneToMillis(fixture), fixture.getTime())
|
2022-02-10 04:50:46 +09:00
|
|
|
t.deepEqual(
|
|
|
|
chronoDateAdd1Minute(fixture),
|
|
|
|
new Date(fixture.getTime() + 60 * 1000),
|
|
|
|
)
|
|
|
|
})
|
2023-03-21 12:22:07 +09:00
|
|
|
|
2024-04-30 17:12:30 +09:00
|
|
|
Napi5Test('Get date', (t) => {
|
|
|
|
const fixture1 = new Date('2024-02-07T18:28:18-0800')
|
|
|
|
t.deepEqual(chronoDateFixtureReturn1(), fixture1)
|
|
|
|
const fixture2 = new Date('2024-02-07T18:28:18+0530')
|
|
|
|
t.deepEqual(chronoDateFixtureReturn2(), fixture2)
|
|
|
|
})
|
|
|
|
|
2023-03-21 12:22:07 +09:00
|
|
|
Napi5Test('Class with getter setter closures', (t) => {
|
|
|
|
const instance = new GetterSetterWithClosures()
|
|
|
|
// @ts-expect-error
|
|
|
|
instance.name = 'Allie'
|
|
|
|
t.pass()
|
|
|
|
// @ts-expect-error
|
|
|
|
t.is(instance.name, `I'm Allie`)
|
|
|
|
// @ts-expect-error
|
|
|
|
t.is(instance.age, 0.3)
|
|
|
|
})
|
2023-05-26 19:28:34 +09:00
|
|
|
|
|
|
|
Napi5Test('Date to chrono::NativeDateTime test', (t) => {
|
|
|
|
const fixture = new Date()
|
|
|
|
t.is(chronoNativeDateTime(fixture), fixture.valueOf())
|
|
|
|
})
|
|
|
|
|
|
|
|
Napi5Test('Date from chrono::NativeDateTime test', (t) => {
|
|
|
|
const fixture = chronoNativeDateTimeReturn()
|
|
|
|
t.true(fixture instanceof Date)
|
|
|
|
t.is(fixture?.toISOString(), '2016-12-23T15:25:59.325Z')
|
|
|
|
})
|
2023-09-14 07:45:14 +09:00
|
|
|
|
|
|
|
const Napi9Test = Number(process.versions.napi) >= 9 ? test : test.skip
|
|
|
|
|
|
|
|
Napi9Test('create symbol for', (t) => {
|
|
|
|
t.is(createSymbolFor('foo'), Symbol.for('foo'))
|
|
|
|
})
|
2023-11-06 12:03:44 +09:00
|
|
|
|
|
|
|
Napi9Test('get module file name', (t) => {
|
2023-11-07 02:46:43 +09:00
|
|
|
if (process.env.WASI_TEST) {
|
2023-11-06 12:03:44 +09:00
|
|
|
t.pass()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.info(getModuleFileName())
|
2024-02-26 16:46:07 +09:00
|
|
|
t.regex(
|
|
|
|
getModuleFileName(),
|
|
|
|
new RegExp(`example.${process.platform}-${process.arch}`),
|
|
|
|
)
|
2023-11-06 12:03:44 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
test('throw syntax error', (t) => {
|
|
|
|
const message = `Syntax Error: Unexpected token '}'`
|
|
|
|
const code = 'InvalidCharacterError'
|
|
|
|
t.throws(
|
|
|
|
() => throwSyntaxError(message, code),
|
|
|
|
{
|
|
|
|
code,
|
|
|
|
instanceOf: SyntaxError,
|
|
|
|
},
|
|
|
|
message,
|
|
|
|
)
|
|
|
|
})
|