Skip to content

Commit

Permalink
Merge pull request #37 from billywhizz/main
Browse files Browse the repository at this point in the history
0.0.17 Release
  • Loading branch information
billywhizz authored Jun 22, 2024
2 parents e5a5002 + ec5bf18 commit d649b17
Show file tree
Hide file tree
Showing 18 changed files with 157 additions and 35 deletions.
14 changes: 7 additions & 7 deletions BOOK.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ gzip compressed binary from github.
on linux/x64

```shell
curl -L -o lo.gz https://github.com/just-js/lo/releases/download/0.0.16-pre/lo-linux-x64.gz
curl -L -o lo.gz https://github.com/just-js/lo/releases/download/0.0.17-pre/lo-linux-x64.gz
```

on linux/arm64

```shell
curl -L -o lo.gz https://github.com/just-js/lo/releases/download/0.0.16-pre/lo-linux-arm64.gz
curl -L -o lo.gz https://github.com/just-js/lo/releases/download/0.0.17-pre/lo-linux-arm64.gz
```

on macos/x64

```shell
curl -L -o lo.gz https://github.com/just-js/lo/releases/download/0.0.16-pre/lo-mac-x64.gz
curl -L -o lo.gz https://github.com/just-js/lo/releases/download/0.0.17-pre/lo-mac-x64.gz
```

on macos/arm64 (apple silicon)

```shell
curl -L -o lo.gz https://github.com/just-js/lo/releases/download/0.0.16-pre/lo-mac-arm64.gz
curl -L -o lo.gz https://github.com/just-js/lo/releases/download/0.0.17-pre/lo-mac-arm64.gz
```

Then, we need to decompress the downloaded file and make it executable
Expand Down Expand Up @@ -122,7 +122,7 @@ let's check the version number of lo and the embedded JS engine it is using
```shell
./lo --version

0.0.16-pre
0.0.17-pre
```

### evaluating JavaScript
Expand Down Expand Up @@ -165,7 +165,7 @@ lo has a very basic repl which you can invoke as follows
⬛⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜⬛
⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛⬛

lo 0.0.16-pre v8 12.4.254.18
lo 0.0.17-pre v8 12.4.254.18
arch x64 os linux
boot 6.02 ms rss 34996224

Expand All @@ -181,7 +181,7 @@ we downloaded.

{
version: {
lo: "0.0.16-pre",
lo: "0.0.17-pre",
v8: "12.4.254.18"
},
...
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CCARGS=-std=c++17 -c -fno-omit-frame-pointer -fno-rtti -fno-exceptions
CARGS=-c -fno-omit-frame-pointer
WARN=-Werror -Wpedantic -Wall -Wextra -Wno-unused-parameter
OPT=-O3
VERSION=0.0.16-pre
VERSION=0.0.17-pre
V8_VERSION=12.4
RUNTIME=lo
LO_HOME=$(shell pwd)
Expand Down
1 change: 1 addition & 0 deletions lib/asm/asm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ import { Compiler } from 'lib/asm/compiler.js'
export { Registers, Compiler, Assembler }

// https://www.chromium.org/chromium-os/developer-library/reference/linux-constants/syscalls/
// https://github.com/GregoryComer/x86-csv/blob/master/x86.csv
2 changes: 1 addition & 1 deletion lib/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ const linux = {
MFD_CLOEXEC: 'i32',
MAP_HUGETLB: 'i32',
MAP_HUGE_SHIFT: 'i32',
MAP_32BIT: 'i32',
// MAP_32BIT: 'i32', // this breaks arm64/linux build, even with -D_GNU_SOURCE defined
MADV_HUGEPAGE: 'i32',
MAP_FIXED: 'i32',
POSIX_FADV_SEQUENTIAL: 'i32', POSIX_FADV_WILLNEED: 'i32', POSIX_FADV_RANDOM: 'i32',
Expand Down
11 changes: 4 additions & 7 deletions lib/core/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -479,14 +479,13 @@ v8::CFunctionInfo infowrite = v8::CFunctionInfo(rcwrite, 4, cargswrite);
v8::CFunction pFwrite = v8::CFunction((const void*)&writeFast, &infowrite);

int32_t write_stringFast(void* p, int32_t p0, struct FastOneByteString* const p1);
v8::CTypeInfo cargswrite_string[4] = {
v8::CTypeInfo cargswrite_string[3] = {
v8::CTypeInfo(v8::CTypeInfo::Type::kV8Value),
v8::CTypeInfo(v8::CTypeInfo::Type::kInt32),
v8::CTypeInfo(v8::CTypeInfo::Type::kSeqOneByteString),
v8::CTypeInfo(v8::CTypeInfo::Type::kInt32),
};
v8::CTypeInfo rcwrite_string = v8::CTypeInfo(v8::CTypeInfo::Type::kInt32);
v8::CFunctionInfo infowrite_string = v8::CFunctionInfo(rcwrite_string, 4, cargswrite_string);
v8::CFunctionInfo infowrite_string = v8::CFunctionInfo(rcwrite_string, 3, cargswrite_string);
v8::CFunction pFwrite_string = v8::CFunction((const void*)&write_stringFast, &infowrite_string);

int32_t putcharFast(void* p, int32_t p0);
Expand Down Expand Up @@ -1122,13 +1121,12 @@ v8::CFunctionInfo infostrnlen = v8::CFunctionInfo(rcstrnlen, 3, cargsstrnlen);
v8::CFunction pFstrnlen = v8::CFunction((const void*)&strnlenFast, &infostrnlen);

uint32_t strnlen_strFast(void* p, struct FastOneByteString* const p0);
v8::CTypeInfo cargsstrnlen_str[3] = {
v8::CTypeInfo cargsstrnlen_str[2] = {
v8::CTypeInfo(v8::CTypeInfo::Type::kV8Value),
v8::CTypeInfo(v8::CTypeInfo::Type::kSeqOneByteString),
v8::CTypeInfo(v8::CTypeInfo::Type::kUint32),
};
v8::CTypeInfo rcstrnlen_str = v8::CTypeInfo(v8::CTypeInfo::Type::kUint32);
v8::CFunctionInfo infostrnlen_str = v8::CFunctionInfo(rcstrnlen_str, 3, cargsstrnlen_str);
v8::CFunctionInfo infostrnlen_str = v8::CFunctionInfo(rcstrnlen_str, 2, cargsstrnlen_str);
v8::CFunction pFstrnlen_str = v8::CFunction((const void*)&strnlen_strFast, &infostrnlen_str);

void syncFast(void* p);
Expand Down Expand Up @@ -2753,7 +2751,6 @@ void Init(Isolate* isolate, Local<ObjectTemplate> target) {
SET_VALUE(isolate, module, "MFD_CLOEXEC", Integer::New(isolate, (int32_t)MFD_CLOEXEC));
SET_VALUE(isolate, module, "MAP_HUGETLB", Integer::New(isolate, (int32_t)MAP_HUGETLB));
SET_VALUE(isolate, module, "MAP_HUGE_SHIFT", Integer::New(isolate, (int32_t)MAP_HUGE_SHIFT));
SET_VALUE(isolate, module, "MAP_32BIT", Integer::New(isolate, (int32_t)MAP_32BIT));
SET_VALUE(isolate, module, "MADV_HUGEPAGE", Integer::New(isolate, (int32_t)MADV_HUGEPAGE));
SET_VALUE(isolate, module, "MAP_FIXED", Integer::New(isolate, (int32_t)MAP_FIXED));
SET_VALUE(isolate, module, "POSIX_FADV_SEQUENTIAL", Integer::New(isolate, (int32_t)POSIX_FADV_SEQUENTIAL));
Expand Down
30 changes: 29 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const write_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
const read_flags = O_RDONLY
const dir_flags = S_IRWXU | S_IRWXG | S_IROTH

const DT_CHR = 2
const DT_DIR = 4
const DT_REG = 8
const default_options = { recursive: true }

function checkMode (val, mode) {
return (val & S_IFMT) === mode
}
Expand All @@ -45,6 +50,29 @@ function file_size (path) {
return size
}

function readdir_sync (path, entries = [], options = default_options) {
const dir = opendir(path)
let next = readdir(dir)
assert(next)
while (next) {
const entry = readEntry(next)
if (!(entry.name === '..' || entry.name === '.')) {
const entry_path = `${path}/${entry.name}`
if (entry.d_type === DT_DIR) {
entries.push({ path, name: entry_path, isDirectory: true })
if (options.recursive) readdir_sync(entry_path, entries, options)
} else if (entry.d_type === DT_REG) {
entries.push({ path, name: entry.name, isFile: true })
} else if (entry.d_type === DT_CHR) {
entries.push({ path, name: entry.name, isChar: true })
}
}
next = readdir(dir)
}
assert(closedir(dir) === 0)
return entries
}

function readEntry (handle) {
readMemory(u8, handle, 19)
const d_ino = dir_view.getUint32(0, true)
Expand Down Expand Up @@ -150,5 +178,5 @@ const mkdir_all_safe = mkDirAllSafe
export {
mkDirAll, rmDirAll, isFile, isDir, readEntry,
write_flags, read_flags, write_mode, dir_flags,
is_dir, is_file, mkdir_all, rmdir_all, mkdir_all_safe, file_size
is_dir, is_file, mkdir_all, rmdir_all, mkdir_all_safe, file_size, readdir_sync
}
12 changes: 11 additions & 1 deletion lib/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,13 @@ function getFastParameterCast (parameter, index, pointers, override) {
}

function getSlowParameterCast (parameter, index, pointers, override) {
// if (parameter === 'pointer64') {
// const pType = pointers[index] || 'void*'
// return ` ${pType} v${index} = reinterpret_cast<${pType}>((uint64_t)Local<BigInt>::Cast(args[${index}])->Uint64Value());`
// }
if (parameter === 'pointer') {
const pType = pointers[index] || 'void*'
// return ` ${pType} v${index} = reinterpret_cast<${pType}>((uint64_t)Local<BigInt>::Cast(args[${index}])->Uint64Value());`
return ` ${pType} v${index} = reinterpret_cast<${pType}>((uint64_t)Local<Integer>::Cast(args[${index}])->Value());`
}
if (parameter === 'string') {
Expand Down Expand Up @@ -193,10 +198,15 @@ function bindings ({ api, includes = [], name, preamble, constants, structs = []

function getFastFunctionDecl (n, api) {
const definition = api[n]
const { parameters, result, name = n, nofast } = definition
const { result, name = n, nofast, override } = definition
if (nofast) {
return ''
}
let parameters = definition.parameters.slice(0)
if (override && override.length && override.length >= parameters.length) {
const overrides_len = override.filter(v => v).length
parameters = parameters.slice(0, parameters.length - overrides_len)
}
if (needsUnwrap(result)) {
let src = `\nvoid ${n}Fast(void* p${(parameters.length || needsUnwrap(definition.result)) ? ', ' : ''}${getParams(definition)});`
src += `\nv8::CTypeInfo cargs${n}[${parameters.length + 2}] = {\n`
Expand Down
16 changes: 12 additions & 4 deletions lib/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const rx = [
[/\n?\s+?([<{])/g, '$1']
]

const { core } = lo
const { read_file } = core

const unsafeCharsRegExp = /[<&]/g
const replaceMatch = (c) => c === "&" ? "&amp;" : "&lt;"
const escape = str => unsafeCharsRegExp.test(str) ? str.replace(unsafeCharsRegExp, replaceMatch) : str;
Expand Down Expand Up @@ -117,13 +120,13 @@ class Parser {
if (type === 'string') {
if (this.rawStrings) {
if (this.depth > 0) {
inner.push(`String.raw\`${sanitize(token.value)}\``)
inner.push(`\$\{String.raw\`${sanitize(token.value)}\`\}`)
} else {
source.push(`html = html + String.raw\`${sanitize(token.value)}\``)
}
} else {
if (this.depth > 0) {
inner.push(`${sanitize(token.value, true)}`)
inner.push(`\$\{${sanitize(token.value, true)}\}`)
} else {
source.push(`html = html + "${sanitize(token.value, true)}"`)
}
Expand All @@ -144,7 +147,11 @@ class Parser {
return
}
if (this.command === 'code') {
source.push(`html += ${value}`)
if (this.depth > 0) {
inner.push(`\$\{${value}\}`)
} else {
source.push(`html = html + ${value}`)
}
return
}
if (this.command === 'arg') {
Expand Down Expand Up @@ -265,7 +272,7 @@ class Parser {
}
}

function compile (template, name = 'template', root = '', opts = {}) {
function compile (template, root = '', opts = {}) {
const { plugins = {}, rawStrings, escape = false } = opts
const tokenizer = new Tokenizer()
tokenizer.tokenize(template)
Expand All @@ -279,6 +286,7 @@ function compile (template, name = 'template', root = '', opts = {}) {
}`)
call = f(escapeHtml)
} else {
// console.log(parser.source.join('\n'))
call = new Function(...parser.args, parser.source.join('\n'))
}
return { call, tokenizer, parser, template }
Expand Down
11 changes: 10 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { net } = lo.load('net')

const { utf8EncodeIntoAtOffset } = lo
const { utf8EncodeIntoAtOffset, addr, assert } = lo

// todo: use the constants from the bindings
const { AF_INET, AF_UNIX, SOCK_STREAM, SOCK_NONBLOCK, EINPROGRESS } = net
Expand Down Expand Up @@ -59,6 +59,12 @@ function sockaddr_in (ip, port) {
return new Uint8Array(buf)
}

function get_sockname (fd, sockaddr) {
u32[0] = 16
assert(net.getsockname(fd, sockaddr, u32) === 0)
return addr(u32)
}

function sockaddr_un (path) {
const u8 = new Uint8Array(102)
const dv = new DataView(u8.buffer)
Expand All @@ -79,12 +85,15 @@ net.pipe = (pipes, flags = O_CLOEXEC) => {

net.inet_aton = inet_aton

const u32 = new Uint32Array(2)
const on = new Uint32Array([1])
const off = new Uint32Array([0])

net.on = on
net.off = off

net.get_sockname = get_sockname

net.types = {
sockaddr_in,
sockaddr_un,
Expand Down
5 changes: 5 additions & 0 deletions lib/net/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ const api = lib_api_typed({
parameters: ['i32', 'i32'],
result: 'i32'
},
getsockname: {
parameters: ['i32', 'buffer', 'u32array'],
pointers: [, 'struct sockaddr*', 'socklen_t*'],
result: 'i32'
},
})

const constants = {
Expand Down
2 changes: 1 addition & 1 deletion lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function normalize (path) {

function join (...args) {
if (args.length === 0) return '.'
if (args.length === 2 && args[1][0] === '/') return normalize(args[1])
// if (args.length === 2 && args[1][0] === '/') return normalize(args[1])
let joined
for (let i = 0; i < args.length; ++i) {
const arg = args[i]
Expand Down
6 changes: 3 additions & 3 deletions lib/proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ if (core.os === 'linux' || core.os === 'mac') {
console.error(err.message)
console.error((new Error(``)).stack)
}
lo.exit(1)
// lo.exit(1)
} else if (pid > 0) {
status[1] = waitpid(pid, status, 0)
if (status[0] === 0) assert(status[1] === pid)
Expand All @@ -106,7 +106,7 @@ if (core.os === 'linux' || core.os === 'mac') {
console.error(err.message)
console.error((new Error(``)).stack)
}
lo.exit(1)
// lo.exit(1)
} else if (pid > 0) {
status[1] = waitpid(pid, status, 0)
if (status[0] === 0) assert(status[1] === pid)
Expand All @@ -127,7 +127,7 @@ if (core.os === 'linux' || core.os === 'mac') {
console.error(err.message)
console.error((new Error(``)).stack)
}
lo.exit(1)
// lo.exit(1)
} else if (pid > 0) {
status[1] = waitpid(pid, status, 0)
if (status[0] === 0) assert(status[1] === pid)
Expand Down
3 changes: 2 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ function bestline_isatty (p) {

let nextline = (isatty(STDIN) && isatty(STDOUT)) ? bestline_isatty : bestline_notatty

async function repl () {
async function repl (ctx) {
// cls(1)
logo()
info()
let line
globalThis.repl = ctx
while (line = nextline(prompt)) {
try {
const len = strnlen(line, MAX_LINE)
Expand Down
12 changes: 10 additions & 2 deletions lib/udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Node {
fd = -1
msghdr = new MsgHdr()
iov = new IoVec()
port = -1
#port = 0
address = '127.0.0.1'
peer_address = undefined

Expand All @@ -87,7 +87,7 @@ class Node {
assert(bind(fd, src, src.length) === 0)
if (loop) assert(loop.add(fd, on_readable, Readable, on_error) === 0)
this.fd = fd
this.port = port
this.#port = port
this.address = address
this.msghdr.payload = this.iov
if (!loop) lo.nextTick(on_readable)
Expand Down Expand Up @@ -123,6 +123,14 @@ class Node {
close(this.fd)
this.fd = -1
}

get port () {
if (this.#port > 0) return this.#port
const src = sockaddr_in(this.address, this.#port)
net.get_sockname(this.fd, src)
this.#port = (new DataView(src.buffer)).getUint16(2, true)
return this.#port
}
}


Expand Down
Loading

0 comments on commit d649b17

Please sign in to comment.