Skip to content

Commit

Permalink
modules working - ugly though
Browse files Browse the repository at this point in the history
  • Loading branch information
billywhizz committed Dec 16, 2020
1 parent 926a14a commit 4e6d5d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ deps
*.a
modules
examples
.just
.just
libs
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ modules: ## download the modules for this release
mv modules-$(RELEASE) modules
rm -f modules.tar.gz

libs: ## download the libs for this release
rm -fr libs
curl -L -o libs.tar.gz https://github.com/just-js/libs/archive/$(RELEASE).tar.gz
tar -zxvf libs.tar.gz
mv libs-$(RELEASE) libs
rm -f libs.tar.gz

examples: ## download the examples for this release
rm -fr examples
curl -L -o examples.tar.gz https://github.com/just-js/examples/archive/$(RELEASE).tar.gz
Expand Down Expand Up @@ -122,6 +129,7 @@ cleanall: ## remove just and build deps
rm -fr deps
rm -f *.gz
rm -fr modules
rm -fr libs
rm -fr examples
make clean

Expand Down
11 changes: 9 additions & 2 deletions just.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ function wrapLibrary (cache = {}) {

function wrapRequire (cache = {}) {
const appRoot = just.sys.cwd()
const { HOME, JUST_TARGET } = just.env()
const justDir = JUST_TARGET || `${HOME}/.just`

function requireNative (path) {
path = `lib/${path}.js`
Expand Down Expand Up @@ -164,7 +166,12 @@ function wrapRequire (cache = {}) {
path = fileName.replace(appRoot, '')
if (path[0] === '/') path = path.slice(1)
module.text = just.builtin(path)
if (!module.text) return
if (!module.text) {
path = `${justDir}/${path.replace('lib/', 'libs/')}`
if (!just.fs.isFile(path)) return
module.text = just.fs.readFile(path)
if (!module.text) return
}
}
cache[fileName] = module
if (ext === 'js') {
Expand Down Expand Up @@ -264,6 +271,7 @@ function main () {
just.fs = library('fs').fs
just.net = library('net').net
just.sys = library('sys').sys
just.env = wrapEnv(just.sys.env)

const { requireNative, require } = wrapRequire(cache)
ArrayBuffer.prototype.writeString = function(str, off = 0) { // eslint-disable-line
Expand Down Expand Up @@ -299,7 +307,6 @@ function main () {
just.require.cache = cache
just.waitForInspector = false

just.env = wrapEnv(just.sys.env)
just.memoryUsage = wrapMemoryUsage(just.sys.memoryUsage)
just.cpuUsage = wrapCpuUsage(just.sys.cpuUsage)
just.rUsage = wrapgetrUsage(just.sys.getrUsage)
Expand Down
22 changes: 16 additions & 6 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ allow merging of modules
check all missing files before compilation
filter duplicates before generating .S file
*/
const { print, error, builtin, path } = just
const { print, error, builtin } = just
const { launch, watch } = just.process
const { cwd } = just.sys
const { compile } = just.vm
Expand Down Expand Up @@ -60,11 +60,14 @@ ${name}_end:
`
}

function generateBuiltins (main, index, libs, embeds, modules) {
function generateBuiltins (main, index, libs, embeds, modules, justDir = '') {
const lines = []
const files = libs.concat(embeds)
// todo - make this a string literal/template
for (const file of files) {
for (let file of files) {
if (file[0] === '/') {
file = file.replace(`${justDir}/`, '').replace('libs', 'lib')
}
const path = file.replace(/[./]/g, '_')
lines.push(`extern char _binary_${path}_start[];`)
lines.push(`extern char _binary_${path}_end[];`)
Expand All @@ -81,7 +84,10 @@ function generateBuiltins (main, index, libs, embeds, modules) {
}
lines.push('}')
lines.push('void register_builtins() {')
for (const file of files) {
for (let file of files) {
if (file[0] === '/') {
file = file.replace(`${justDir}/`, '').replace('libs', 'lib')
}
const path = file.replace(/[./]/g, '_')
lines.push(` just::builtins_add("${file}", _binary_${path}_start, _binary_${path}_end - _binary_${path}_start);`)
}
Expand Down Expand Up @@ -163,7 +169,11 @@ async function run (config = {}, { cleanall = false, clean = false, dump = false
}
if (config.target !== 'just') {
for (const fileName of config.libs) {
links[fileName] = linkFile(`${appDir}/${fileName}`, fileName)
if (fileName[0] === '/') {
links[fileName] = linkFile(fileName, fileName.replace(`${justDir}/`, '').replace('libs', 'lib'))
} else {
links[fileName] = linkFile(`${appDir}/${fileName}`, fileName)
}
}
for (const fileName of config.embeds) {
links[fileName] = linkFile(`${appDir}/${fileName}`, fileName)
Expand Down Expand Up @@ -206,7 +216,7 @@ async function run (config = {}, { cleanall = false, clean = false, dump = false
}
writeFile('builtins.S', ArrayBuffer.fromString(Object.keys(links).map(k => links[k]).join('')))
if (!isDir('lib')) mkdir('lib')
const src = generateBuiltins(main, index, config.libs, config.embeds, modules)
const src = generateBuiltins(main, index, config.libs, config.embeds, modules, justDir)
writeFile('main.h', ArrayBuffer.fromString(src))
for (const fileName of runtime.embeds) {
if (!isFile(fileName)) {
Expand Down

0 comments on commit 4e6d5d2

Please sign in to comment.