Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions assets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as ff from "firefly-as/assembly";

export function render(): void {
ff.clearScreen(ff.Color.White);
ff.drawTriangle(
ff.Point.new(50, 20),
ff.Point.new(30, 50),
ff.Point.new(70, 50),
ff.Style.new(ff.Color.LightBlue, ff.Color.DarkBlue, 1)
);
}
3 changes: 3 additions & 0 deletions assets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
4 changes: 4 additions & 0 deletions assets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "assemblyscript/std/assembly.json",
"include": ["./**/*.ts"]
}
17 changes: 16 additions & 1 deletion src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn cmd_new(args: &NewArgs) -> Result<()> {
Lang::Go => new_go(&args.name).context("new Go project")?,
Lang::Rust => new_rust(&args.name).context("new Rust project")?,
Lang::Zig => new_zig(&args.name).context("new Zig project")?,
Lang::TS => bail!("TypeScript is not supported yet"),
Lang::AS | Lang::TS => new_as(&args.name).context("new AssemblyScript project")?,
Lang::C => new_c(&args.name).context("new C project")?,
Lang::Cpp => new_cpp(&args.name).context("new C++ project")?,
Lang::Python => bail!("Python is not supported yet"),
Expand Down Expand Up @@ -80,6 +80,7 @@ fn parse_lang(lang: &str) -> Result<Lang> {
"go" | "golang" => Lang::Go,
"rust" | "rs" => Lang::Rust,
"zig" => Lang::Zig,
"as" | "assemblyscript" => Lang::AS,
"ts" | "typescript" | "js" | "javascript" => Lang::TS,
"cpp" | "c++" => Lang::Cpp,
"python" | "py" => Lang::Python,
Expand Down Expand Up @@ -174,6 +175,20 @@ fn new_moon(name: &str) -> Result<()> {
Ok(())
}

/// Create a new [AssemblyScript] project.
///
/// [AssemblyScript]: https://www.assemblyscript.org/
fn new_as(name: &str) -> Result<()> {
let mut c = Commander::default();
c.cd(name)?;
c.copy_asset(&["package.json"], "package.json")?;
c.run(&["npm", "install", "--save", "assemblyscript"])?;
c.run(&["npm", "install", "--save", "firefly-as"])?;
c.copy_asset(&["assembly", "tsconfig.json"], "tsconfig.json")?;
c.copy_asset(&["assembly", "index.ts"], "index.ts")?;
Ok(())
}

#[derive(Default)]
struct Commander<'a> {
root: Option<&'a Path>,
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub enum Lang {
Go,
Rust,
Zig,
AS,
TS,
C,
Cpp,
Expand Down
42 changes: 42 additions & 0 deletions src/langs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn build_bin(config: &Config, args: &BuildArgs) -> anyhow::Result<()> {
Lang::Go => build_go(config),
Lang::Rust => build_rust(config),
Lang::Zig => build_zig(config),
Lang::AS => build_as(config),
Lang::TS => build_ts(config),
Lang::C => build_c(config),
Lang::Cpp => build_cpp(config),
Expand Down Expand Up @@ -63,6 +64,15 @@ fn detect_lang(root: &Path) -> anyhow::Result<Lang> {
if root.join("build.zig.zon").exists() {
return Ok(Lang::Zig);
}
if root.join("asconfig.json").exists() {
return Ok(Lang::AS);
}
if root.join("assembly").join("index.ts").exists() {
return Ok(Lang::AS);
}
if root.join("assembly").join("tsconfig.json").exists() {
return Ok(Lang::AS);
}
if root.join("package.json").exists() {
return Ok(Lang::TS);
}
Expand Down Expand Up @@ -390,6 +400,38 @@ fn build_lua(config: &Config) -> anyhow::Result<()> {
Ok(())
}

fn build_as(config: &Config) -> anyhow::Result<()> {
check_installed("AssemblyScript", "npx", "--version")?;
let mut cmd_args = vec![
"asc",
"assembly",
"--use",
"abort=~lib/firefly-as/assembly/stubs/handleAbort",
"--outFile",
"main.wasm",
];
if let Some(additional_args) = &config.compile_args {
for arg in additional_args {
cmd_args.push(arg.as_str());
}
} else {
cmd_args.push("--optimize");
cmd_args.push("--noAssert");
}
let output = Command::new("npx")
.args(cmd_args)
.current_dir(&config.root_path)
.output()
.context("run asc assembly")?;
check_output(&output)?;

let from_path = config.root_path.join("main.wasm");
let out_path = config.rom_path.join(BIN);
std::fs::copy(&from_path, out_path).context("copy wasm binary")?;
std::fs::remove_file(from_path).context("remove wasm file")?;
Ok(())
}

fn build_ts(_config: &Config) -> anyhow::Result<()> {
todo!("TypeScript is not supported yet")
}
Expand Down