Skip to content
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ The build bootstraps the `.bs` CLI sources with the BynixScript compiler from th
`bynixscript` development dependency, then bundles the compiled JavaScript with
esbuild into `dist/index.min.cjs`. `npm test` also checks that every CLI source
compiles through the same compiler path.
The build also bundles the shared compiler with the browser integration entry
into `dist/browser.js`; browser configuration uses the default extensions.
### Using CDN <a name="using-cdn-get-started"></a>
```html
<bynix>
Expand Down
407 changes: 27 additions & 380 deletions js/browser.js

Large diffs are not rendered by default.

40 changes: 1 addition & 39 deletions js/bsc.js
Original file line number Diff line number Diff line change
@@ -1,39 +1 @@
const fs = require('fs');

const defaultConfig = {
extension: {
primary: '.bys',
secondary: '.bynixscript',
module: '.mbs'
},
readFolder: './',
toFolder: './',
watch: false,
allowJs: false,
strict: false,
translate: 'javascript'
};

let config;

try {
const data = fs.readFileSync('./bsc.json', 'utf8');
config = JSON.parse(data);
} catch (err) {
config = defaultConfig;
}

const extensions = {
primary: config.extension?.primary || defaultConfig.extension.primary,
secondary: config.extension?.secondary || defaultConfig.extension.secondary,
module: config.extension?.module || defaultConfig.extension.module
};

const readFolder = config.readFolder || defaultConfig.readFolder;
const toFolder = config.toFolder || defaultConfig.toFolder;
const watch = config.watch || defaultConfig.watch;
const allowJs = config.allowJs || defaultConfig.allowJs;
const strict = config.strict || defaultConfig.strict;
const translate = config.translate || defaultConfig.translate;

module.exports = { defaultConfig, config, extensions, toFolder, readFolder, watch, allowJs, strict, translate };
module.exports = require('./config.js').loadConfig('./bsc.json', '.bys')
40 changes: 1 addition & 39 deletions js/bsconfig.js
Original file line number Diff line number Diff line change
@@ -1,39 +1 @@
const fs = require('fs');

const defaultConfig = {
extension: {
primary: '.bs',
secondary: '.bynixscript',
module: '.mbs'
},
readFolder: './',
toFolder: './',
watch: false,
allowJs: false,
strict: false,
translate: 'javascript'
};

let config;

try {
const data = fs.readFileSync('./bsconfig.json', 'utf8');
config = JSON.parse(data);
} catch (err) {
config = defaultConfig;
}

const extensions = {
primary: config.extension?.primary || defaultConfig.extension.primary,
secondary: config.extension?.secondary || defaultConfig.extension.secondary,
module: config.extension?.module || defaultConfig.extension.module
};

const readFolder = config.readFolder || defaultConfig.readFolder;
const toFolder = config.toFolder || defaultConfig.toFolder;
const watch = config.watch || defaultConfig.watch;
const allowJs = config.allowJs || defaultConfig.allowJs;
const strict = config.strict || defaultConfig.strict;
const translate = config.translate || defaultConfig.translate;

module.exports = { defaultConfig, config, extensions, toFolder, readFolder, watch, allowJs, strict, translate };
module.exports = require('./config.js').loadConfig('./bsconfig.json', '.bs')
31 changes: 31 additions & 0 deletions js/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const fs = require('node:fs');

function loadConfig(filename, primary) {
const defaultConfig = {
extension: { primary, secondary: '.bynixscript', module: '.mbs' },
readFolder: './', toFolder: './', watch: false,
allowJs: false, strict: false, translate: 'javascript'
}
var config
try {
config = JSON.parse(fs.readFileSync(filename, 'utf8'));
} catch (error) {
config = defaultConfig;
}
const extensions = {
primary: config.extension?.primary || defaultConfig.extension.primary,
secondary: config.extension?.secondary || defaultConfig.extension.secondary,
module: config.extension?.module || defaultConfig.extension.module
}
return {
defaultConfig, config, extensions,
readFolder: config.readFolder || defaultConfig.readFolder,
toFolder: config.toFolder || defaultConfig.toFolder,
watch: config.watch || defaultConfig.watch,
allowJs: config.allowJs || defaultConfig.allowJs,
strict: config.strict || defaultConfig.strict,
translate: config.translate || defaultConfig.translate
}
}

module.exports = { loadConfig }
66 changes: 24 additions & 42 deletions js/dirProcess.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,29 @@
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const { translate } = require('./parser.js');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const { compileSource } = require('../scripts/compile.cjs');

function readAndProcessFilesInDirectory(directory) {
// Baca semua file dalam direktori
fs.readdir(directory, (err, files) => {
if (err) {
console.error("Error reading directory:", err);
return;
fs.readdir(directory, (error, files) => {
if (error) {
console.error('Error reading directory:', error);
return;
}
files.filter(file => path.extname(file) === '.bs').forEach(file => {
const filePath = path.join(directory, file);
fs.readFile(filePath, 'utf8', (readError, data) => {
if (readError) {
console.error(`Error reading file ${file}:`, readError)
return;
}

// Filter hanya file .bs
const bsFiles = files.filter(file => path.extname(file) === '.bs');

// Proses setiap file .bs
bsFiles.forEach(file => {
const filePath = path.join(directory, file);

// Baca isi file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(`Error reading file ${file}:`, err);
return;
}

// Konversi "print" menjadi "console.log"
let processedData = translate(code);

// Eksekusi kode yang telah diproses menggunakan vm
try {
const script = new vm.Script(processedData);
script.runInThisContext();
} catch (err) {
console.error(`Error executing file ${file}:`, err);
}
});
});
});
try {
new vm.Script(compileSource(data), { filename: filePath }).runInThisContext()
} catch (executionError) {
console.error(`Error executing file ${file}:`, executionError)
}
})
})
})
}

// Panggil fungsi ini dengan direktori yang berisi file .bs
readAndProcessFilesInDirectory('./src');

// Ekspor fungsi jika dibutuhkan untuk digunakan di file lain
module.exports = { readAndProcessFilesInDirectory };
module.exports = { readAndProcessFilesInDirectory }
23 changes: 8 additions & 15 deletions js/download.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
const https = require('https');
const https = require('node:https');

function getDownloads(packageName, time) {
const url = `https://api.npmjs.org/downloads/point/last-${time}/${packageName}`;

https.get(url, (res) => {
https.get(url, response => {
let data = '';

res.on('data', (chunk) => {
data += chunk;
});

res.on('end', () => {
response.on('data', chunk => { data += chunk });
response.on('end', () => {
const jsonData = JSON.parse(data);
console.log(`Package ${packageName} downloaded ${jsonData.downloads} times.`);
});
}).on('error', (e) => {
console.error(`Error: ${e.message}`);
});
console.log(`Package ${packageName} downloaded ${jsonData.downloads} times.`)
})
}).on('error', error => { console.error(`Error: ${error.message}`) });
}

module.exports = { https, getDownloads };
module.exports = { https, getDownloads }
80 changes: 28 additions & 52 deletions js/error.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,34 @@
function stackParsing(filename, errorMsg, errorStack, code, type, modulLocation) {
if (code !== undefined && code !== null) {
const stackLines = errorStack.split('\n');
let codeLines = code.split('\n');

const errorLine = stackLines[0];
const parts = errorLine.split(':');
let line;
let column
let tabs;
let arrow;
let errorCode;
if (type === "SyntaxError") {
const errorLine = stackLines[0];
const parts = errorLine.split(':');
line = parseInt(parts[1].trim());
let l = line - 2;
errorCode = codeLines[l].trim();

return `${type}: ${errorMsg}\n at file ${filename}\n\nSnippet:\n${errorCode}\n`;
} else if (type === "TypeError" || type === "ReferenceError" || type === "RangeError" || type === "URIError" || type === "EvalError") {
const errorLine = stackLines[5];
const parts = errorLine.split(':');
line = parseInt(parts[1].trim());
column = parseInt(parts[2].trim());

if (column > 6) {
column -= 6;
}

let l = line - 1;
const codeNoTrim = codeLines[l]
errorCode = codeLines[l].trim();
const spaces = codeNoTrim.match(/^ +/);
const spacesCount = spaces ? spaces[0].length : 0;
let x = column - spacesCount - 1;
if (x < 0) {
x = Math.abs(x);
x -= 2;
}
tabs = ' '.repeat(x);
arrow = tabs + '^';

return `${type}: ${errorMsg}\n at file ${filename}\n at line ${line}, column ${column}\n\nSnippet:\n${errorCode}\n${arrow}`;
} else {
let commonErr = errorMsg
commonErr = commonErr.replace(/\sstack/g, '');
commonErr = commonErr.replace(/bst\.js/g, filename);
commonErr = commonErr.replace(/bsr\.js/g, filename);
commonErr = commonErr.replace(/bsp\.js/g, filename);
return `${type}: ${commonErr}`;
if (code === undefined || code === null) {
return errorMsg
}
const stackLines = errorStack.split('\n');
const codeLines = code.split('\n');
if (type === 'SyntaxError') {
const line = parseInt(stackLines[0].split(':')[1].trim());
const errorCode = codeLines[line - 2].trim();
return `${type}: ${errorMsg}\n at file ${filename}\n\nSnippet:\n${errorCode}\n`
} else if (type === 'TypeError' || type === 'ReferenceError' || type === 'RangeError' || type === 'URIError' || type === 'EvalError') {
const parts = stackLines[5].split(':');
const line = parseInt(parts[1].trim());
let column = parseInt(parts[2].trim());
if (column > 6) {
column -= 6;
}
const codeNoTrim = codeLines[line - 1];
const errorCode = codeNoTrim.trim();
const spaces = codeNoTrim.match(/^ +/);
const spacesCount = spaces ? spaces[0].length : 0;
let offset = column - spacesCount - 1;
if (offset < 0) {
offset = Math.abs(offset) - 2;
}
const arrow = ' '.repeat(offset) + '^';
return `${type}: ${errorMsg}\n at file ${filename}\n at line ${line}, column ${column}\n\nSnippet:\n${errorCode}\n${arrow}`
} else {
return errorMsg;
const commonErr = errorMsg.replace(/\sstack/g, '').replace(/bst\.js|bsr\.js|bsp\.js/g, filename);
return `${type}: ${commonErr}`
}
}

module.exports = { stackParsing };
module.exports = { stackParsing }
Loading
Loading