Skip to content

Commit 54aad7b

Browse files
authored
Merge pull request #57 from firefly-zero/assemblyscript
AssemblyScript support
2 parents d5f3529 + 5a6fbe7 commit 54aad7b

6 files changed

Lines changed: 77 additions & 1 deletion

File tree

assets/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as ff from "firefly-as/assembly";
2+
3+
export function render(): void {
4+
ff.clearScreen(ff.Color.White);
5+
ff.drawTriangle(
6+
ff.Point.new(50, 20),
7+
ff.Point.new(30, 50),
8+
ff.Point.new(70, 50),
9+
ff.Style.new(ff.Color.LightBlue, ff.Color.DarkBlue, 1)
10+
);
11+
}

assets/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

assets/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "assemblyscript/std/assembly.json",
3+
"include": ["./**/*.ts"]
4+
}

src/commands/new.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn cmd_new(args: &NewArgs) -> Result<()> {
2525
Lang::Go => new_go(&args.name).context("new Go project")?,
2626
Lang::Rust => new_rust(&args.name).context("new Rust project")?,
2727
Lang::Zig => new_zig(&args.name).context("new Zig project")?,
28-
Lang::TS => bail!("TypeScript is not supported yet"),
28+
Lang::AS | Lang::TS => new_as(&args.name).context("new AssemblyScript project")?,
2929
Lang::C => new_c(&args.name).context("new C project")?,
3030
Lang::Cpp => new_cpp(&args.name).context("new C++ project")?,
3131
Lang::Python => bail!("Python is not supported yet"),
@@ -80,6 +80,7 @@ fn parse_lang(lang: &str) -> Result<Lang> {
8080
"go" | "golang" => Lang::Go,
8181
"rust" | "rs" => Lang::Rust,
8282
"zig" => Lang::Zig,
83+
"as" | "assemblyscript" => Lang::AS,
8384
"ts" | "typescript" | "js" | "javascript" => Lang::TS,
8485
"cpp" | "c++" => Lang::Cpp,
8586
"python" | "py" => Lang::Python,
@@ -174,6 +175,20 @@ fn new_moon(name: &str) -> Result<()> {
174175
Ok(())
175176
}
176177

178+
/// Create a new [AssemblyScript] project.
179+
///
180+
/// [AssemblyScript]: https://www.assemblyscript.org/
181+
fn new_as(name: &str) -> Result<()> {
182+
let mut c = Commander::default();
183+
c.cd(name)?;
184+
c.copy_asset(&["package.json"], "package.json")?;
185+
c.run(&["npm", "install", "--save", "assemblyscript"])?;
186+
c.run(&["npm", "install", "--save", "firefly-as"])?;
187+
c.copy_asset(&["assembly", "tsconfig.json"], "tsconfig.json")?;
188+
c.copy_asset(&["assembly", "index.ts"], "index.ts")?;
189+
Ok(())
190+
}
191+
177192
#[derive(Default)]
178193
struct Commander<'a> {
179194
root: Option<&'a Path>,

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ pub enum Lang {
179179
Go,
180180
Rust,
181181
Zig,
182+
AS,
182183
TS,
183184
C,
184185
Cpp,

src/langs.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn build_bin(config: &Config, args: &BuildArgs) -> anyhow::Result<()> {
2424
Lang::Go => build_go(config),
2525
Lang::Rust => build_rust(config),
2626
Lang::Zig => build_zig(config),
27+
Lang::AS => build_as(config),
2728
Lang::TS => build_ts(config),
2829
Lang::C => build_c(config),
2930
Lang::Cpp => build_cpp(config),
@@ -63,6 +64,15 @@ fn detect_lang(root: &Path) -> anyhow::Result<Lang> {
6364
if root.join("build.zig.zon").exists() {
6465
return Ok(Lang::Zig);
6566
}
67+
if root.join("asconfig.json").exists() {
68+
return Ok(Lang::AS);
69+
}
70+
if root.join("assembly").join("index.ts").exists() {
71+
return Ok(Lang::AS);
72+
}
73+
if root.join("assembly").join("tsconfig.json").exists() {
74+
return Ok(Lang::AS);
75+
}
6676
if root.join("package.json").exists() {
6777
return Ok(Lang::TS);
6878
}
@@ -390,6 +400,38 @@ fn build_lua(config: &Config) -> anyhow::Result<()> {
390400
Ok(())
391401
}
392402

403+
fn build_as(config: &Config) -> anyhow::Result<()> {
404+
check_installed("AssemblyScript", "npx", "--version")?;
405+
let mut cmd_args = vec![
406+
"asc",
407+
"assembly",
408+
"--use",
409+
"abort=~lib/firefly-as/assembly/stubs/handleAbort",
410+
"--outFile",
411+
"main.wasm",
412+
];
413+
if let Some(additional_args) = &config.compile_args {
414+
for arg in additional_args {
415+
cmd_args.push(arg.as_str());
416+
}
417+
} else {
418+
cmd_args.push("--optimize");
419+
cmd_args.push("--noAssert");
420+
}
421+
let output = Command::new("npx")
422+
.args(cmd_args)
423+
.current_dir(&config.root_path)
424+
.output()
425+
.context("run asc assembly")?;
426+
check_output(&output)?;
427+
428+
let from_path = config.root_path.join("main.wasm");
429+
let out_path = config.rom_path.join(BIN);
430+
std::fs::copy(&from_path, out_path).context("copy wasm binary")?;
431+
std::fs::remove_file(from_path).context("remove wasm file")?;
432+
Ok(())
433+
}
434+
393435
fn build_ts(_config: &Config) -> anyhow::Result<()> {
394436
todo!("TypeScript is not supported yet")
395437
}

0 commit comments

Comments
 (0)