From 8ffe4a70dd0c57fa3c36beed8f6342dadb22df85 Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Sat, 8 Jul 2023 16:39:41 +0300 Subject: [PATCH] feat(vm/compiler): embed templates to executable --- vm/compiler/compiler.go | 15 +++++++++------ .../compiler/templates}/amd64-386/arithmetic.asm | 0 .../compiler/templates}/amd64-386/effects.asm | 0 .../compiler/templates}/amd64-386/flowcontrol.asm | 0 .../compiler/templates}/amd64-386/gmdls.asm | 0 .../compiler/templates}/amd64-386/library.asm | 0 .../compiler/templates}/amd64-386/library.h | 0 .../templates}/amd64-386/output_sound.asm | 0 .../compiler/templates}/amd64-386/patch.asm | 0 .../compiler/templates}/amd64-386/player.asm | 0 .../compiler/templates}/amd64-386/player.h | 0 .../compiler/templates}/amd64-386/sinks.asm | 0 .../compiler/templates}/amd64-386/sources.asm | 0 .../compiler/templates}/amd64-386/structs.asm | 0 .../compiler/templates}/wasm/arithmetic.wat | 0 .../compiler/templates}/wasm/effects.wat | 0 .../compiler/templates}/wasm/output_sound.wat | 0 .../compiler/templates}/wasm/patch.wat | 0 .../compiler/templates}/wasm/player.wat | 0 .../compiler/templates}/wasm/sinks.wat | 0 .../compiler/templates}/wasm/sources.wat | 0 21 files changed, 9 insertions(+), 6 deletions(-) rename {templates => vm/compiler/templates}/amd64-386/arithmetic.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/effects.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/flowcontrol.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/gmdls.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/library.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/library.h (100%) rename {templates => vm/compiler/templates}/amd64-386/output_sound.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/patch.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/player.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/player.h (100%) rename {templates => vm/compiler/templates}/amd64-386/sinks.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/sources.asm (100%) rename {templates => vm/compiler/templates}/amd64-386/structs.asm (100%) rename {templates => vm/compiler/templates}/wasm/arithmetic.wat (100%) rename {templates => vm/compiler/templates}/wasm/effects.wat (100%) rename {templates => vm/compiler/templates}/wasm/output_sound.wat (100%) rename {templates => vm/compiler/templates}/wasm/patch.wat (100%) rename {templates => vm/compiler/templates}/wasm/player.wat (100%) rename {templates => vm/compiler/templates}/wasm/sinks.wat (100%) rename {templates => vm/compiler/templates}/wasm/sources.wat (100%) diff --git a/vm/compiler/compiler.go b/vm/compiler/compiler.go index 23bcd8f..87bf3a9 100644 --- a/vm/compiler/compiler.go +++ b/vm/compiler/compiler.go @@ -2,10 +2,9 @@ package compiler import ( "bytes" + "embed" "fmt" - "path" "path/filepath" - "runtime" "text/template" "github.com/Masterminds/sprig" @@ -21,9 +20,11 @@ type Compiler struct { RowSync bool } +//go:embed templates/amd64-386/* templates/wasm/* +var templateFS embed.FS + // New returns a new compiler using the default .asm templates func New(os string, arch string, output16Bit bool, rowsync bool) (*Compiler, error) { - _, myname, _, _ := runtime.Caller(0) var subdir string if arch == "386" || arch == "amd64" { subdir = "amd64-386" @@ -32,9 +33,11 @@ func New(os string, arch string, output16Bit bool, rowsync bool) (*Compiler, err } else { return nil, fmt.Errorf("compiler.New failed, because only amd64, 386 and wasm archs are supported (targeted architecture was %v)", arch) } - templateDir := filepath.Join(path.Dir(myname), "..", "..", "templates", subdir) - compiler, err := NewFromTemplates(os, arch, output16Bit, rowsync, templateDir) - return compiler, err + tmpl, err := template.New("base").Funcs(sprig.TxtFuncMap()).ParseFS(templateFS, "templates/"+subdir+"/*.*") + if err != nil { + return nil, fmt.Errorf(`could not create templates: %v`, err) + } + return &Compiler{Template: tmpl, OS: os, Arch: arch, RowSync: rowsync, Output16Bit: output16Bit}, nil } func NewFromTemplates(os string, arch string, output16Bit bool, rowsync bool, templateDirectory string) (*Compiler, error) { diff --git a/templates/amd64-386/arithmetic.asm b/vm/compiler/templates/amd64-386/arithmetic.asm similarity index 100% rename from templates/amd64-386/arithmetic.asm rename to vm/compiler/templates/amd64-386/arithmetic.asm diff --git a/templates/amd64-386/effects.asm b/vm/compiler/templates/amd64-386/effects.asm similarity index 100% rename from templates/amd64-386/effects.asm rename to vm/compiler/templates/amd64-386/effects.asm diff --git a/templates/amd64-386/flowcontrol.asm b/vm/compiler/templates/amd64-386/flowcontrol.asm similarity index 100% rename from templates/amd64-386/flowcontrol.asm rename to vm/compiler/templates/amd64-386/flowcontrol.asm diff --git a/templates/amd64-386/gmdls.asm b/vm/compiler/templates/amd64-386/gmdls.asm similarity index 100% rename from templates/amd64-386/gmdls.asm rename to vm/compiler/templates/amd64-386/gmdls.asm diff --git a/templates/amd64-386/library.asm b/vm/compiler/templates/amd64-386/library.asm similarity index 100% rename from templates/amd64-386/library.asm rename to vm/compiler/templates/amd64-386/library.asm diff --git a/templates/amd64-386/library.h b/vm/compiler/templates/amd64-386/library.h similarity index 100% rename from templates/amd64-386/library.h rename to vm/compiler/templates/amd64-386/library.h diff --git a/templates/amd64-386/output_sound.asm b/vm/compiler/templates/amd64-386/output_sound.asm similarity index 100% rename from templates/amd64-386/output_sound.asm rename to vm/compiler/templates/amd64-386/output_sound.asm diff --git a/templates/amd64-386/patch.asm b/vm/compiler/templates/amd64-386/patch.asm similarity index 100% rename from templates/amd64-386/patch.asm rename to vm/compiler/templates/amd64-386/patch.asm diff --git a/templates/amd64-386/player.asm b/vm/compiler/templates/amd64-386/player.asm similarity index 100% rename from templates/amd64-386/player.asm rename to vm/compiler/templates/amd64-386/player.asm diff --git a/templates/amd64-386/player.h b/vm/compiler/templates/amd64-386/player.h similarity index 100% rename from templates/amd64-386/player.h rename to vm/compiler/templates/amd64-386/player.h diff --git a/templates/amd64-386/sinks.asm b/vm/compiler/templates/amd64-386/sinks.asm similarity index 100% rename from templates/amd64-386/sinks.asm rename to vm/compiler/templates/amd64-386/sinks.asm diff --git a/templates/amd64-386/sources.asm b/vm/compiler/templates/amd64-386/sources.asm similarity index 100% rename from templates/amd64-386/sources.asm rename to vm/compiler/templates/amd64-386/sources.asm diff --git a/templates/amd64-386/structs.asm b/vm/compiler/templates/amd64-386/structs.asm similarity index 100% rename from templates/amd64-386/structs.asm rename to vm/compiler/templates/amd64-386/structs.asm diff --git a/templates/wasm/arithmetic.wat b/vm/compiler/templates/wasm/arithmetic.wat similarity index 100% rename from templates/wasm/arithmetic.wat rename to vm/compiler/templates/wasm/arithmetic.wat diff --git a/templates/wasm/effects.wat b/vm/compiler/templates/wasm/effects.wat similarity index 100% rename from templates/wasm/effects.wat rename to vm/compiler/templates/wasm/effects.wat diff --git a/templates/wasm/output_sound.wat b/vm/compiler/templates/wasm/output_sound.wat similarity index 100% rename from templates/wasm/output_sound.wat rename to vm/compiler/templates/wasm/output_sound.wat diff --git a/templates/wasm/patch.wat b/vm/compiler/templates/wasm/patch.wat similarity index 100% rename from templates/wasm/patch.wat rename to vm/compiler/templates/wasm/patch.wat diff --git a/templates/wasm/player.wat b/vm/compiler/templates/wasm/player.wat similarity index 100% rename from templates/wasm/player.wat rename to vm/compiler/templates/wasm/player.wat diff --git a/templates/wasm/sinks.wat b/vm/compiler/templates/wasm/sinks.wat similarity index 100% rename from templates/wasm/sinks.wat rename to vm/compiler/templates/wasm/sinks.wat diff --git a/templates/wasm/sources.wat b/vm/compiler/templates/wasm/sources.wat similarity index 100% rename from templates/wasm/sources.wat rename to vm/compiler/templates/wasm/sources.wat