Skip to content

Commit ede042d

Browse files
authored
feat: Expose version as global constants (AssemblyScript#1765)
1 parent af53c48 commit ede042d

File tree

8 files changed

+56
-5
lines changed

8 files changed

+56
-5
lines changed

NOTICE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ under the licensing terms detailed in LICENSE:
3636
* Peter Salomonsen <petersalomonsen@runbox.com>
3737
* ookangzheng <git-ed@runbox.no>
3838
* yjhmelody <yjh465402634@gmail.com>
39+
* bnbarak <bn.barak@gmail.com>
3940

4041
Portions of this software are derived from third-party works licensed under
4142
the following terms:
@@ -61,6 +62,6 @@ the following terms:
6162
The 3-Clause BSD License (https://opensource.org/licenses/BSD-3-Clause)
6263

6364
* Arm Optimized Routines: https://github.com/ARM-software/optimized-routines
64-
65+
6566
Copyright (c) Arm Limited
6667
The MIT License (https://opensource.org/licenses/MIT)

cli/asc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ exports.main = function main(argv, options, callback) {
239239
options = {};
240240
}
241241

242+
// Bundle semantic version
243+
let bundleMinorVersion = 0, bundleMajorVersion = 0, bundlePatchVersion = 0;
244+
const versionParts = (exports.version || "").split(".");
245+
if (versionParts.length === 3) {
246+
bundleMajorVersion = parseInt(versionParts[0]) | 0;
247+
bundleMinorVersion = parseInt(versionParts[1]) | 0;
248+
bundlePatchVersion = parseInt(versionParts[2]) | 0;
249+
}
250+
242251
const stdout = options.stdout || process.stdout;
243252
const stderr = options.stderr || process.stderr;
244253
const readFile = options.readFile || readFileNode;
@@ -416,6 +425,7 @@ exports.main = function main(argv, options, callback) {
416425
assemblyscript.setPedantic(compilerOptions, opts.pedantic);
417426
assemblyscript.setLowMemoryLimit(compilerOptions, opts.lowMemoryLimit >>> 0);
418427
assemblyscript.setExportRuntime(compilerOptions, opts.exportRuntime);
428+
assemblyscript.setBundleVersion(compilerOptions, bundleMajorVersion, bundleMinorVersion, bundlePatchVersion);
419429
if (!opts.stackSize && opts.runtime == "incremental") {
420430
opts.stackSize = assemblyscript.DEFAULT_STACK_SIZE;
421431
}

src/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ export namespace CommonNames {
178178
export const ASC_FEATURE_MULTI_VALUE = "ASC_FEATURE_MULTI_VALUE";
179179
export const ASC_FEATURE_GC = "ASC_FEATURE_GC";
180180
export const ASC_FEATURE_MEMORY64 = "ASC_FEATURE_MEMORY64";
181+
export const ASC_VERSION_MAJOR = "ASC_VERSION_MAJOR";
182+
export const ASC_VERSION_MINOR = "ASC_VERSION_MINOR";
183+
export const ASC_VERSION_PATCH = "ASC_VERSION_PATCH";
181184
// classes
182185
export const I8 = "I8";
183186
export const I16 = "I16";

src/compiler.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ export class Options {
251251
exportRuntime: bool = false;
252252
/** Stack size in bytes, if using a stack. */
253253
stackSize: i32 = 0;
254+
/** Semantic major bundle version from root package.json */
255+
bundleMajorVersion: i32 = 0;
256+
/** Semantic minor bundle version from root package.json */
257+
bundleMinorVersion: i32 = 0;
258+
/** Semantic patch bundle version from root package.json */
259+
bundlePatchVersion: i32 = 0;
254260

255261
/** Hinted optimize level. Not applied by the compiler itself. */
256262
optimizeLevelHint: i32 = 0;
@@ -1509,7 +1515,7 @@ export class Compiler extends DiagnosticEmitter {
15091515
if (!this.compileFunctionBody(instance, stmts)) {
15101516
stmts.push(module.unreachable());
15111517
}
1512-
1518+
15131519
this.currentFlow = previousFlow;
15141520

15151521
// create the function
@@ -7533,7 +7539,7 @@ export class Compiler extends DiagnosticEmitter {
75337539
expr = module.local_tee(local.index, expr, ftype.isManaged);
75347540
}
75357541
}
7536-
7542+
75377543
return expr;
75387544
}
75397545

src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,18 @@ export function setStackSize(options: Options, stackSize: i32): void {
142142
options.stackSize = stackSize;
143143
}
144144

145+
/** Sets the bundle semantic version. */
146+
export function setBundleVersion(
147+
options: Options,
148+
bundleMajorVersion: i32,
149+
bundleMinorVersion: i32,
150+
bundlePatchVersion: i32,
151+
): void {
152+
options.bundleMajorVersion = bundleMajorVersion;
153+
options.bundleMinorVersion = bundleMinorVersion;
154+
options.bundlePatchVersion = bundlePatchVersion;
155+
}
156+
145157
/** Sign extension operations. */
146158
export const FEATURE_SIGN_EXTENSION = Feature.SIGN_EXTENSION;
147159
/** Mutable global imports and exports. */

src/program.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,12 @@ export class Program extends DiagnosticEmitter {
10211021
i64_new(options.lowMemoryLimit, 0));
10221022
this.registerConstantInteger(CommonNames.ASC_EXPORT_RUNTIME, Type.bool,
10231023
i64_new(options.exportRuntime ? 1 : 0, 0));
1024+
this.registerConstantInteger(CommonNames.ASC_VERSION_MAJOR, Type.i32,
1025+
i64_new(options.bundleMajorVersion));
1026+
this.registerConstantInteger(CommonNames.ASC_VERSION_MINOR, Type.i32,
1027+
i64_new(options.bundleMinorVersion));
1028+
this.registerConstantInteger(CommonNames.ASC_VERSION_PATCH, Type.i32,
1029+
i64_new(options.bundlePatchVersion));
10241030

10251031
// register feature hints
10261032
this.registerConstantInteger(CommonNames.ASC_FEATURE_SIGN_EXTENSION, Type.bool,
@@ -3092,8 +3098,8 @@ export class File extends Element {
30923098

30933099
/** Creates an imported namespace from this file. */
30943100
asAliasNamespace(
3095-
name: string,
3096-
parent: Element,
3101+
name: string,
3102+
parent: Element,
30973103
localIdentifier: IdentifierExpression
30983104
): Namespace {
30993105
var declaration = this.program.makeNativeNamespaceDeclaration(name);

tests/compiler/asc-constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ ASC_FEATURE_REFERENCE_TYPES;
1616
ASC_FEATURE_MULTI_VALUE;
1717
ASC_FEATURE_GC;
1818
ASC_FEATURE_MEMORY64;
19+
20+
ASC_VERSION_MAJOR;
21+
ASC_VERSION_MINOR;
22+
ASC_VERSION_PATCH;

tests/compiler/asc-constants.untouched.wat

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
(global $~lib/ASC_FEATURE_MULTI_VALUE i32 (i32.const 0))
2020
(global $~lib/ASC_FEATURE_GC i32 (i32.const 0))
2121
(global $~lib/ASC_FEATURE_MEMORY64 i32 (i32.const 0))
22+
(global $~lib/ASC_VERSION_MAJOR i32 (i32.const 0))
23+
(global $~lib/ASC_VERSION_MINOR i32 (i32.const 0))
24+
(global $~lib/ASC_VERSION_PATCH i32 (i32.const 0))
2225
(global $~lib/memory/__data_end i32 (i32.const 8))
2326
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 16392))
2427
(global $~lib/memory/__heap_base i32 (i32.const 16392))
@@ -59,6 +62,12 @@
5962
drop
6063
i32.const 0
6164
drop
65+
i32.const 0
66+
drop
67+
i32.const 0
68+
drop
69+
i32.const 0
70+
drop
6271
)
6372
(func $~start
6473
call $start:asc-constants

0 commit comments

Comments
 (0)