Skip to content

Commit

Permalink
feat: added hooking.js within the features
Browse files Browse the repository at this point in the history
and assigned an id to each feature for
future implementation of function hooking
(sharing instantiated server > client and client > server objects).
The hooking.js feature simply adds text before
the file only if certain features match
  • Loading branch information
XenoS-ITA committed May 26, 2023
1 parent 31c4421 commit fe95500
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/features/arrowFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let match = VerEx()
.then("{")

let ArrowFunction = {
id: "arrowFunction",
from: match,
to: function(originalFile) {
let file = originalFile
Expand Down
46 changes: 27 additions & 19 deletions src/features/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import VerEx from "verbal-expressions";
import { ReplaceFunctionEnding } from "../modules/functions";
import { sliceLine } from "../modules/linesManipulation";
import { MatchAllRegex } from "../modules/regex";
import { AddHook } from "./hooking";
import "../modules/string"

function classIterator(fileData, matchIndices) {
Expand Down Expand Up @@ -49,9 +50,9 @@ function classIterator(fileData, matchIndices) {
for (e of opening) {
countEnds += line.occurrences(e)
}

countEnds -= line.occurrences("end")

if (countEnds === 0) {
inFunction = false;
}
Expand Down Expand Up @@ -90,6 +91,7 @@ let classExtendsMatch = VerEx()
.then("{")

let Class = {
id: "class",
from: classMatch,
to: function(file) {
let matchIndices = MatchAllRegex(file, classMatch).map(x => x.index);
Expand All @@ -109,30 +111,14 @@ let Class = {
return obj
end
-- Type function wrapper to add classes type (is here in order not to add a line to the beginning of the file and thus shred the stack traceback lines)
-- We check to see if there is already a class that has overridden the type function so that there is no stack overflow
if not _type then
_type = type
type = function(var)
local realType = _type(var)
if realType == "table" and var.type then
return var.type
else
return realType
end
end
end
Prototype$1 = {type = "$1",
*/

if (matchIndices.length > 0) {
file = classIterator(file, matchIndices)

file = file.replace(classMatch, dedent`
$1=function(...)local a=setmetatable({},{__index=function(self,b)return Prototype$1[b]end})if a.constructor then a:constructor(...)end;return a end;if not _type then _type=type;type=function(b)local realType=_type(b)if realType=="table"and b.type then return b.type else return realType end end end;Prototype$1={type = "$1",
$1=function(...)local a=setmetatable({},{__index=function(self,b)return Prototype$1[b]end})if a.constructor then a:constructor(...)end;return a end;Prototype$1={type = "$1",
`)
}

Expand All @@ -141,6 +127,7 @@ let Class = {
}

let ClassExtends = {
id: "classExtends",
from: classExtendsMatch,
to: function(file) {
let matchIndices = MatchAllRegex(file, classExtendsMatch).map(x => x.index);
Expand Down Expand Up @@ -184,4 +171,25 @@ let ClassExtends = {
}
}


/* Beautified code, code is minified to help with error debugging
-- Type function wrapper to add classes type (is here in order not to add a line to the beginning of the file and thus shred the stack traceback lines)
-- We check to see if there is already a class that has overridden the type function so that there is no stack overflow
if not _type then
_type = type
type = function(var)
local realType = _type(var)
if realType == "table" and var.type then
return var.type
else
return realType
end
end
end
*/
AddHook(["class", "classExtends"], 'if not _type then _type=type;type=function(b)local realType=_type(b)if realType=="table"and b.type then return b.type else return realType end end end')

export {Class, ClassExtends}
1 change: 1 addition & 0 deletions src/features/decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ let decoratorsVerEx = VerEx()
)

let Decorators = {
id: "decorators",
from: match,
to: function(file) {
MatchAllRegex(file, match).map(match => {
Expand Down
1 change: 1 addition & 0 deletions src/features/defaultValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ let extractDefaultValues = VerEx()
.endCapture()

let DefaultValue = {
id: "defaultValue",
from: triggerMatch,
to: function(file) {
let matches = MatchAllRegex(file, triggerMatch);
Expand Down
32 changes: 32 additions & 0 deletions src/features/hooking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
let hooks = []

function AddHook(id, content) { // id can be an array with multiple valid id
hooks.push({id: id, content: content+";"})
}

function HookFunctionsOfMatched(body, matchedFeatures) {
for (let matched of matchedFeatures) {
for (let hook of hooks) {
// Check if we need to skip the hook creation
if (typeof hook.id == "string") {
if (hook.id != matched) {
continue
}
} else {
let found = hook.id.some( id => {
if (id == matched) return true
})

if (found) {
continue
}
}

body = hook.content + body
}
}

return body
}

export {AddHook, HookFunctionsOfMatched}
1 change: 1 addition & 0 deletions src/features/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let match = VerEx()
.endCapture()

let New = {
id: "new",
from: match,
to: function(file) {
return file.replace(match, "$1")
Expand Down
1 change: 1 addition & 0 deletions src/features/notEqual.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
let NotEqual = {
id: "notEqual",
from: "!=",
to: "~="
}
Expand Down
1 change: 1 addition & 0 deletions src/features/typeChecking.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ let extractTypes = VerEx()
.endCapture()

let TypeChecking = {
id: "typeChecking",
from: triggerMatch,
to: function(file) {
let matches = MatchAllRegex(file, triggerMatch);
Expand Down
1 change: 1 addition & 0 deletions src/features/unpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let match = VerEx()
.endCapture()

let Unpack = {
id: "unpack",
from: match,
to: function(file) {
MatchAllRegex(file, match).map(x => {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/linesManipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ function getChars(fileData, lineNumber) {
}

function sliceLine(string, lineStart, lineEnd) {
if (lineStart && !lineEnd) {
if (lineStart && !lineEnd) { // works also with only the starting line (slice from the starting line to the end of the file)
let chars = getChars(string, lineStart)

return string.slice(chars)
} else {
} else { // normal line to line slice
let charsStart = getChars(string, lineStart)
let charsEnd = getChars(string, lineEnd)

Expand Down
6 changes: 6 additions & 0 deletions src/modules/postProcessing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import VerEx from "verbal-expressions";
import glob from "glob"
import fs from "fs"
import {Features} from "../index"
import { HookFunctionsOfMatched } from "../features/hooking";

function ResolveFile(resourcePath, file) {
if (file.includes("*") > 0) { // If have some glob
Expand All @@ -13,6 +14,7 @@ function ResolveFile(resourcePath, file) {

function PostProcess(resourceName, file, type, write = true) {
let fileData = (type == "build") ? fs.readFileSync(file, "utf-8") : file;
let matchedFeatures = []

for (let feature of Features) {
if (typeof feature.from == "string") {
Expand All @@ -23,6 +25,8 @@ function PostProcess(resourceName, file, type, write = true) {
feature.from.addModifier("g")

if (match) {
matchedFeatures.push(feature.id)

if (typeof feature.to == "string") {
fileData = fileData.replace(feature.from, feature.to)
} else {
Expand All @@ -32,6 +36,8 @@ function PostProcess(resourceName, file, type, write = true) {
}
}

fileData = HookFunctionsOfMatched(fileData, matchedFeatures)

if (type == "build") {
let outputFileDir = file.replace(resourceName, resourceName+"/build") // Add "/build" after the resource name
let outputDir = outputFileDir.replace(VerEx().word().then(".lua"), "") // Remove "filename.lua"
Expand Down

0 comments on commit fe95500

Please sign in to comment.