Start work on ability to load wasm languages from native lib, via wasmtime
This commit is contained in:
parent
8883d43bee
commit
3f1a7f9cd4
19 changed files with 2989 additions and 284 deletions
|
|
@ -124,6 +124,7 @@ fn run() -> Result<()> {
|
|||
.arg(&debug_arg)
|
||||
.arg(&debug_build_arg)
|
||||
.arg(&debug_graph_arg)
|
||||
.arg(Arg::with_name("wasm").long("wasm").help("use wasm file"))
|
||||
.arg(Arg::with_name("debug-xml").long("xml").short("x"))
|
||||
.arg(
|
||||
Arg::with_name("stat")
|
||||
|
|
@ -353,6 +354,7 @@ fn run() -> Result<()> {
|
|||
let debug_xml = matches.is_present("debug-xml");
|
||||
let quiet = matches.is_present("quiet");
|
||||
let time = matches.is_present("time");
|
||||
let wasm = matches.is_present("wasm");
|
||||
let edits = matches
|
||||
.values_of("edits")
|
||||
.map_or(Vec::new(), |e| e.collect());
|
||||
|
|
@ -379,8 +381,18 @@ fn run() -> Result<()> {
|
|||
let should_track_stats = matches.is_present("stat");
|
||||
let mut stats = parse::Stats::default();
|
||||
|
||||
let mut wasm_language = None;
|
||||
if wasm {
|
||||
let (language_name, wasm_file) = wasm::load_language_wasm_file(¤t_dir)?;
|
||||
let engine = tree_sitter::wasmtime::Engine::default();
|
||||
let mut context = tree_sitter::WasmStore::new(engine);
|
||||
wasm_language = Some(context.load_language(&language_name, &wasm_file));
|
||||
std::mem::forget(context);
|
||||
}
|
||||
|
||||
for path in paths {
|
||||
let path = Path::new(&path);
|
||||
|
||||
let language =
|
||||
loader.select_language(path, ¤t_dir, matches.value_of("scope"))?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use super::wasm;
|
||||
use anyhow::Context;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
env, fs,
|
||||
|
|
@ -58,19 +57,8 @@ pub fn serve(grammar_path: &Path, open_in_browser: bool) {
|
|||
env::var("TREE_SITTER_PLAYGROUND_ADDR").unwrap_or("127.0.0.1".to_owned()),
|
||||
port
|
||||
);
|
||||
let (grammar_name, language_wasm) = wasm::load_language_wasm_file(&grammar_path).unwrap();
|
||||
let server = Server::http(&addr).expect("Failed to start web server");
|
||||
let grammar_name = wasm::get_grammar_name(&grammar_path.join("src"))
|
||||
.with_context(|| "Failed to get wasm filename")
|
||||
.unwrap();
|
||||
let wasm_filename = format!("tree-sitter-{}.wasm", grammar_name);
|
||||
let language_wasm = fs::read(grammar_path.join(&wasm_filename))
|
||||
.with_context(|| {
|
||||
format!(
|
||||
"Failed to read {}. Run `tree-sitter build-wasm` first.",
|
||||
wasm_filename
|
||||
)
|
||||
})
|
||||
.unwrap();
|
||||
let url = format!("http://{}", addr);
|
||||
println!("Started playground on: {}", url);
|
||||
if open_in_browser {
|
||||
|
|
|
|||
|
|
@ -1,14 +1,31 @@
|
|||
use super::generate::parse_grammar::GrammarJSON;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::{
|
||||
ffi::{OsStr, OsString},
|
||||
fs,
|
||||
path::Path,
|
||||
process::Command,
|
||||
};
|
||||
use which::which;
|
||||
|
||||
const EMSCRIPTEN_TAG: &'static str = concat!("emscripten/emsdk:", env!("EMSCRIPTEN_VERSION"));
|
||||
|
||||
pub fn get_grammar_name(src_dir: &Path) -> Result<String> {
|
||||
pub fn load_language_wasm_file(language_dir: &Path) -> Result<(String, Vec<u8>)> {
|
||||
let grammar_name = get_grammar_name(&language_dir)
|
||||
.with_context(|| "Failed to get wasm filename")
|
||||
.unwrap();
|
||||
let wasm_filename = format!("tree-sitter-{}.wasm", grammar_name);
|
||||
let contents = fs::read(language_dir.join(&wasm_filename)).with_context(|| {
|
||||
format!(
|
||||
"Failed to read {}. Run `tree-sitter build-wasm` first.",
|
||||
wasm_filename
|
||||
)
|
||||
})?;
|
||||
Ok((grammar_name, contents))
|
||||
}
|
||||
|
||||
pub fn get_grammar_name(language_dir: &Path) -> Result<String> {
|
||||
let src_dir = language_dir.join("src");
|
||||
let grammar_json_path = src_dir.join("grammar.json");
|
||||
let grammar_json = fs::read_to_string(&grammar_json_path)
|
||||
.with_context(|| format!("Failed to read grammar file {:?}", grammar_json_path))?;
|
||||
|
|
@ -18,8 +35,7 @@ pub fn get_grammar_name(src_dir: &Path) -> Result<String> {
|
|||
}
|
||||
|
||||
pub fn compile_language_to_wasm(language_dir: &Path, force_docker: bool) -> Result<()> {
|
||||
let src_dir = language_dir.join("src");
|
||||
let grammar_name = get_grammar_name(&src_dir)?;
|
||||
let grammar_name = get_grammar_name(&language_dir)?;
|
||||
let output_filename = format!("tree-sitter-{}.wasm", grammar_name);
|
||||
|
||||
let emcc_bin = if cfg!(windows) { "emcc.bat" } else { "emcc" };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue