-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathpackage_runner.das
More file actions
225 lines (211 loc) · 8.13 KB
/
Copy pathpackage_runner.das
File metadata and controls
225 lines (211 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
options gen2
options rtti
options indenting = 4
options no_global_variables = false
options strict_smart_pointers = true
module package_runner shared public
require daslib/ast
require daslib/debugger
require daslib/fio
require daslib/daspkg public
// Results from running a .das_package script
struct ResolveResult {
tag : string
branch : string
source : string // non-empty = redirect to different repo
}
struct PackageDependency {
source : string
version_constraint : string
}
struct PackageBuildInfo {
command : string
is_cmake : bool
is_custom : bool
}
struct PackageReleaseInfo {
name : string
main_script : string
wasm_main_script : string
include_globs : array<string>
include_if_missing_globs : array<string>
exclude_globs : array<string>
forced_modules : array<string>
native_dlls : array<string>
bundle_id : string
emcc_args : array<string>
embed_paths : array<string>
web_shell : string
wasm_build_command : string
wasm_archives : array<string>
requires_jit : bool
include_symbols : bool
external_files : array<string>
}
// Run the `package()` function of a .das_package and extract metadata
def run_das_package_meta(das_package_path : string; var meta : PackageMeta) : bool {
let args : array<string>
let ok = run_das_package(das_package_path, "package", args) $(ctx) {
var p = unsafe(get_context_global_variable(ctx, "_pkg_meta"))
if (p != null) {
let src = unsafe(reinterpret<PackageMeta?>(p))
meta.pkg_name = clone_string(src.pkg_name)
meta.author = clone_string(src.author)
meta.description = clone_string(src.description)
meta.source = clone_string(src.source)
meta.license = clone_string(src.license)
meta.min_sdk = clone_string(src.min_sdk)
for (t in src.tags) {
meta.tags |> push(clone_string(t))
}
}
}
return ok
}
// Run the `resolve()` function of a .das_package and extract download info
def run_das_package_resolve(das_package_path : string; sdk_version, version : string; var result : ResolveResult) : bool {
let args <- [sdk_version, version]
let ok = run_das_package(das_package_path, "resolve", args) $(ctx) {
var p = unsafe(get_context_global_variable(ctx, "_download_ref"))
if (p != null) {
let ref = unsafe(reinterpret<string?>(p))
result.tag = clone_string(*ref)
}
p = unsafe(get_context_global_variable(ctx, "_download_kind"))
if (p != null) {
let kind = *unsafe(reinterpret<DownloadKind?>(p))
if (kind == DownloadKind.branch) {
result.branch = result.tag
result.tag = ""
}
}
p = unsafe(get_context_global_variable(ctx, "_download_source"))
if (p != null) {
let src = unsafe(reinterpret<string?>(p))
result.source = clone_string(*src)
}
}
return ok
}
// Run the `dependencies()` function of a .das_package and extract deps
def run_das_package_deps(das_package_path : string; version : string; var deps : array<PackageDependency>) : bool {
let args <- [version]
let ok = run_das_package(das_package_path, "dependencies", args) $(ctx) {
var p = unsafe(get_context_global_variable(ctx, "_dependencies"))
if (p != null) {
let arr = unsafe(reinterpret<array<Dependency>?>(p))
for (d in *arr) {
deps |> emplace(PackageDependency(source = clone_string(d.source), version_constraint = clone_string(d.version_constraint)))
}
}
}
return ok
}
// Run the `release()` function of a .das_package and extract release info
def run_das_package_release(das_package_path : string; var info : PackageReleaseInfo) : bool {
let args : array<string>
let ok = run_das_package(das_package_path, "release", args) $(ctx) {
var p = unsafe(get_context_global_variable(ctx, "_release_spec"))
if (p != null) {
let src = unsafe(reinterpret<ReleaseSpec?>(p))
info.name = clone_string(src.name)
info.main_script = clone_string(src.main_script)
info.wasm_main_script = clone_string(src.wasm_main_script)
for (g in src.include_globs) {
info.include_globs |> push(clone_string(g))
}
for (g in src.include_if_missing_globs) {
info.include_if_missing_globs |> push(clone_string(g))
}
for (g in src.exclude_globs) {
info.exclude_globs |> push(clone_string(g))
}
for (m in src.forced_modules) {
info.forced_modules |> push(clone_string(m))
}
for (d in src.native_dlls) {
info.native_dlls |> push(clone_string(d))
}
info.bundle_id = clone_string(src.bundle_id)
for (a in src.emcc_args) {
info.emcc_args |> push(clone_string(a))
}
for (e in src.embed_paths) {
info.embed_paths |> push(clone_string(e))
}
info.web_shell = clone_string(src.web_shell)
info.wasm_build_command = clone_string(src.wasm_build_command)
for (a in src.wasm_archives) {
info.wasm_archives |> push(clone_string(a))
}
info.requires_jit = src.requires_jit
info.include_symbols = src.include_symbols
for (e in src.external_files) {
info.external_files |> push(clone_string(e))
}
}
}
return ok
}
// Run the `build()` function of a .das_package and extract build info
def run_das_package_build(das_package_path : string; var info : PackageBuildInfo) : bool {
let args : array<string>
let ok = run_das_package(das_package_path, "build", args) $(ctx) {
var p = unsafe(get_context_global_variable(ctx, "_build_kind"))
if (p != null) {
let kind = *unsafe(reinterpret<BuildKind?>(p))
info.is_cmake = kind == BuildKind.cmake
info.is_custom = kind == BuildKind.custom
}
p = unsafe(get_context_global_variable(ctx, "_build_command"))
if (p != null) {
let cmd = unsafe(reinterpret<string?>(p))
info.command = clone_string(*cmd)
}
}
return ok
}
// Internal: compile, simulate, call function, read state
def private run_das_package(das_package_path : string; func_name : string; args : array<string>; blk : block<(ctx : smart_ptr<Context>) : void>) : bool {
if (!fexist(das_package_path)) {
to_log(LOG_ERROR, "Error: {das_package_path} not found\n")
return false
}
var ok = false
var inscope access <- make_file_access("")
using() $(var mg : ModuleGroup) {
using() $(var cop : CodeOfPolicies) {
cop.threadlock_context = true
cop.export_all = true
compile_file(das_package_path, access, unsafe(addr(mg)), cop) $(compiled; program; issues) {
if (!compiled) {
to_log(LOG_ERROR, "Error compiling {das_package_path}:\n{issues}\n")
return
}
simulate(program) $(simulated; ctx; serrors) {
if (!simulated) {
to_log(LOG_ERROR, "Error simulating {das_package_path}:\n{serrors}\n")
return
}
try {
unsafe {
let n = length(args)
if (n == 0) {
invoke_in_context(ctx, func_name)
} elif (n == 1) {
invoke_in_context(ctx, func_name, args[0])
} elif (n == 2) {
invoke_in_context(ctx, func_name, args[0], args[1])
}
}
} recover {
pass
}
invoke(blk, ctx)
ok = true
}
}
}
}
return ok
}