Last active
December 30, 2024 19:04
-
-
Save billywhizz/6e9f0e74b725ced3c9894baa6ad55e60 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
git clone https://github.com/just-js/lo.git | |
cd lo/ | |
make lo | |
export LO_HOME=$(pwd) | |
./lo build binding bestlines | |
cd .. | |
lo/lo build runtime test-dlopen | |
./test-dlopen |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const bindings = ['core'] | |
const libs = [] | |
const target = 'test-dlopen' | |
const index = 'test-dlopen.js' | |
const embeds = ['lib/bestlines/bestlines.so'] | |
export default { bindings, libs, embeds, target, index } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { stringify } from 'lib/stringify.js' | |
// create a memfd. the name can be whatever you want and is not shared among processes | |
const fd = lo.core.memfd_create('/vfs/bestlines.so', lo.core.MFD_CLOEXEC) | |
// look up the start and size of the embedded shared library | |
const start = lo.core.dlsym(0, '_binary_lib_bestlines_bestlines_so_start') | |
const size = lo.core.dlsym(0, '_binary_lib_bestlines_bestlines_so_end') - start | |
// wrap the memory (zero copy) in an arraybuffer. by default, the memory won't | |
// be freed when the ab goes out of scope, which is what we want in this case | |
const b = new Uint8Array(lo.wrap_memory(start, size)) | |
// if we just want to load it from disk, we can do this | |
//const b = lo.core.read_file('./lib/bestlines/bestlines.so') | |
// write the shared library contents to the memfd | |
lo.core.write(fd, b, b.length) | |
// use dlopen to open the shared library from the procfs virtual file that is | |
// created for the memfd | |
const handle = lo.core.dlopen(`/proc/self/fd/${fd}`, 2) | |
// lookup the binding register symbol in the shared library and load the | |
// binding using it | |
const lib = lo.library(lo.core.dlsym(handle, '_register_bestlines')) | |
// we now have a "lo" binding loaded and ready to use. the same as loading a | |
// shared library from disk | |
console.log(stringify(lib)) | |
/* | |
# dumps the bestlines library to the console | |
{ | |
bestlines: { | |
bestline: "bestline (...0)", | |
bestline_raw: "bestline_raw (...0)", | |
cls: "cls (...0)", | |
add: "add (...0)", | |
save: "save (...0)", | |
load: "load (...0)" | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment