Skip to content

Commit

Permalink
Fix formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
JckXia committed Sep 5, 2021
1 parent dbbafcc commit 4986dce
Showing 1 changed file with 114 additions and 97 deletions.
211 changes: 114 additions & 97 deletions test/functionreference.js
Original file line number Diff line number Diff line change
@@ -1,140 +1,157 @@
'use strict';
"use strict";

const assert = require('assert');
const async_hook = require('async_hooks')
const assert = require("assert");
const async_hook = require("async_hooks");

module.exports = require('./common').runTest(binding => {
module.exports = require("./common").runTest((binding) => {
test(binding.functionreference);
});

function installAsyncHook() {
let id;
let destroyed;
let hook;
const events = []
return new Promise((res, reject) =>{
console.log('Installing async hook')

const interval = setInterval(() => {
if(destroyed) {
hook.disable()
clearInterval(interval)
res(events)
}
},10)

hook = async_hook.createHook({
init(asyncId, type, triggerAsyncId, resource) {

if (id === undefined && type === 'func_ref_resources') {
id = asyncId;
events.push({ eventName: 'init', type, triggerAsyncId, resource });
}
},
before(asyncId) {
if (asyncId === id) {
events.push({ eventName: 'before' });
}
},
after(asyncId) {
if (asyncId === id) {
events.push({ eventName: 'after' });
}
},
destroy(asyncId) {

if (asyncId === id) {
events.push({ eventName: 'destroy' });
destroyed = true;
}
}
}).enable();
const events = [];
return new Promise((res, reject) => {
const interval = setInterval(() => {
if (destroyed) {
hook.disable();
clearInterval(interval);
res(events);
}
}, 10);

hook = async_hook
.createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (id === undefined && type === "func_ref_resources") {
id = asyncId;
events.push({ eventName: "init", type, triggerAsyncId, resource });
}
},
before(asyncId) {
if (asyncId === id) {
events.push({ eventName: "before" });
}
},
after(asyncId) {
if (asyncId === id) {
events.push({ eventName: "after" });
}
},
destroy(asyncId) {
if (asyncId === id) {
events.push({ eventName: "destroy" });
destroyed = true;
}
},
})
.enable();
});
}

function canConstructRefFromExistingRef(binding){
function canConstructRefFromExistingRef(binding) {
const testFunc = () => 240;
assert(binding.ConstructWithMove(testFunc) === 240)
assert(binding.ConstructWithMove(testFunc) === 240);
}

function canCallFunctionWithDifferentOverloads(binding){
let outsideRef = {}
const testFunc = (a,b) => a * a - b * b
const testFuncB = (a,b,c) => a + b - c * c
const testFuncC = (a,b,c) => { outsideRef.a = a; outsideRef.b = b; outsideRef.c = c }
const testFuncD = (a,b,c,d) => { outsideRef.result = a + b * c - d; return outsideRef.result}

assert(binding.CallWithVec(testFunc, 5,4) === testFunc(5,4))
assert(binding.CallWithInitList(testFuncB,2,4,5) === testFuncB(2,4,5))

binding.CallWithRecvVector(testFuncC,outsideRef,1,2,4)
assert(outsideRef.a === 1 && outsideRef.b === 2 && outsideRef.c === 4)

outsideRef = {}
binding.CallWithRecvInitList(testFuncC, outsideRef, 1,2,4)
assert(outsideRef.a === 1 && outsideRef.b === 2 && outsideRef.c === 4)

outsideRef = {}
binding.CallWithRecvArgc(testFuncD, outsideRef, 2,4,5,6)
assert(outsideRef.result === testFuncD(2,4,5,6))
function canCallFunctionWithDifferentOverloads(binding) {
let outsideRef = {};
const testFunc = (a, b) => a * a - b * b;
const testFuncB = (a, b, c) => a + b - c * c;
const testFuncC = (a, b, c) => {
outsideRef.a = a;
outsideRef.b = b;
outsideRef.c = c;
};
const testFuncD = (a, b, c, d) => {
outsideRef.result = a + b * c - d;
return outsideRef.result;
};

assert(binding.CallWithVec(testFunc, 5, 4) === testFunc(5, 4));
assert(binding.CallWithInitList(testFuncB, 2, 4, 5) === testFuncB(2, 4, 5));

binding.CallWithRecvVector(testFuncC, outsideRef, 1, 2, 4);
assert(outsideRef.a === 1 && outsideRef.b === 2 && outsideRef.c === 4);

outsideRef = {};
binding.CallWithRecvInitList(testFuncC, outsideRef, 1, 2, 4);
assert(outsideRef.a === 1 && outsideRef.b === 2 && outsideRef.c === 4);

outsideRef = {};
binding.CallWithRecvArgc(testFuncD, outsideRef, 2, 4, 5, 6);
assert(outsideRef.result === testFuncD(2, 4, 5, 6));
}

async function canCallAsyncFunctionWithDifferentOverloads(binding){

const testFunc = () => 2100
const testFuncB = (a,b,c,d) => a+b+c+d
let hook = installAsyncHook()
binding.AsyncCallWithInitList(testFunc)
async function canCallAsyncFunctionWithDifferentOverloads(binding) {
const testFunc = () => 2100;
const testFuncB = (a, b, c, d) => a + b + c + d;
let hook = installAsyncHook();
binding.AsyncCallWithInitList(testFunc);
let triggerAsyncId = async_hook.executionAsyncId();
let res = await hook
let res = await hook;
assert.deepStrictEqual(res, [
{ eventName: 'init',
type: 'func_ref_resources',
{
eventName: "init",
type: "func_ref_resources",
triggerAsyncId: triggerAsyncId,
resource: { } },
{ eventName: 'before' },
{ eventName: 'after' },
{ eventName: 'destroy' }
resource: {},
},
{ eventName: "before" },
{ eventName: "after" },
{ eventName: "destroy" },
]);

hook = installAsyncHook()
hook = installAsyncHook();
triggerAsyncId = async_hook.executionAsyncId();
assert(binding.AsyncCallWithVector(testFuncB, 2,4,5,6) === testFuncB(2,4,5,6))
res = await hook
assert(
binding.AsyncCallWithVector(testFuncB, 2, 4, 5, 6) === testFuncB(2, 4, 5, 6)
);
res = await hook;
assert.deepStrictEqual(res, [
{ eventName: 'init',
type: 'func_ref_resources',
{
eventName: "init",
type: "func_ref_resources",
triggerAsyncId: triggerAsyncId,
resource: { } },
{ eventName: 'before' },
{ eventName: 'after' },
{ eventName: 'destroy' }
resource: {},
},
{ eventName: "before" },
{ eventName: "after" },
{ eventName: "destroy" },
]);

hook = installAsyncHook()
hook = installAsyncHook();
triggerAsyncId = async_hook.executionAsyncId();
assert(binding.AsyncCallWithArgv(testFuncB, 2,4,5,6) === testFuncB(2,4,5,6))
assert(
binding.AsyncCallWithArgv(testFuncB, 2, 4, 5, 6) === testFuncB(2, 4, 5, 6)
);
}
function test(binding) {
const e = new Error('foobar');
const functionMayThrow = () => { throw e; };
const classMayThrow = class { constructor() { throw e; } };
const e = new Error("foobar");
const functionMayThrow = () => {
throw e;
};
const classMayThrow = class {
constructor() {
throw e;
}
};

const newRef = binding.CreateFuncRefWithNew(120);
assert(newRef.getValue() === 120);

const newRefWithVecArg = binding.CreateFuncRefWithNewVec(80);
assert(newRefWithVecArg.getValue() === 80);

assert.throws(() => {
binding.call(functionMayThrow);
}, /foobar/);
assert.throws(() => {
binding.construct(classMayThrow);
}, /foobar/);

canConstructRefFromExistingRef(binding)
canCallFunctionWithDifferentOverloads(binding)
canCallAsyncFunctionWithDifferentOverloads(binding)


canConstructRefFromExistingRef(binding);
canCallFunctionWithDifferentOverloads(binding);
canCallAsyncFunctionWithDifferentOverloads(binding);
}

0 comments on commit 4986dce

Please sign in to comment.