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

fix: method naming conflicts #215

Closed
67 changes: 67 additions & 0 deletions lib/build-tools/near-bindgen-exporter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions src/build-tools/near-bindgen-exporter.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import * as t from "@babel/types";
import { readFileSync } from 'fs'
import path from 'path'

export default function () {
return {
visitor: {
ClassDeclaration(path) {
const cFunctions = getCFunctions()
let classNode = path.node;
if (classNode.decorators && classNode.decorators[0].expression.callee.name == 'NearBindgen') {
let classId = classNode.id;
Expand All @@ -25,6 +28,7 @@ export default function () {
}
}
for (let method of Object.keys(contractMethods)) {
validateMethod(method, cFunctions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This validating pattern is cool!

path.insertAfter(t.exportNamedDeclaration(t.functionDeclaration(t.identifier(method), [], t.blockStatement([
// const _state = Counter._getState();
t.variableDeclaration('let', [t.variableDeclarator(t.identifier('_state'), t.callExpression(t.memberExpression(classId, t.identifier('_getState')), []))]),
Expand Down Expand Up @@ -67,3 +71,78 @@ export default function () {
}
};
}

function transformCBuilderToMatrix(builderRaw){
let lines = builderRaw.split('\n');
lines = lines.map((line) => [...line])
return lines;
}


function collectCFunction(x, y, cMatrix){
let cFunc = [];
let line = cMatrix[x];
let i = y - 1;
let newY = 0;
while(i > -1){
if(line[i] != " " && line[i] != "(" && line[i] != "*"){

cFunc.push(line[i]);
i= i - 1;
}else {
newY = i + 1;
i = -1;

}
}
return cFunc.length > 0 ? {name: cFunc.reverse().join(''), coords: {x, y: newY}} : null;
}

function collectCFunctions(cMatrix){
let cFunctions = [];
for (let i=0; i<cMatrix.length; i++){
for(let j = 0; j < cMatrix[i].length; j++){
if (cMatrix[i][j] === "("){
const cFunc = collectCFunction(i, j, cMatrix);
cFunctions.push(cFunc)
}
}
}


return cFunctions.filter((cFunc) => cFunc != null)
}

function groupCFunctions(cFunctions){
let newCFunctions = new Map();
cFunctions.forEach(fun => {
let existFun = newCFunctions.get(fun.name);
if(existFun){
newCFunctions.set(fun.name, [...existFun, fun.coords])
}else {
newCFunctions.set(fun.name, [fun.coords])
}
})

return newCFunctions;
}

function getCFunctions(){
const builderRaw = readFileSync(path.resolve('../cli/builder/builder.c'), 'utf-8')
let cMatrix = transformCBuilderToMatrix(builderRaw);
const cFunctions = groupCFunctions(collectCFunctions(cMatrix));
// TODO get coords from regex match
// const regex = new RegExp(/(?<=JS_SetPropertyStr\(ctx, env, ").+?(?=", JS_NewCFunction\(ctx,)/gm)
// const cFunctions = builderRaw.match(regex)
// return cFunctions

return cFunctions;
}

function validateMethod(method, cFunctions) {
const includeMethod = cFunctions.get(method)
if(includeMethod){
throw new Error(`Method ${method} is reserved by Near SDK, use another naming`)
}
}