-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
graaljs.js
143 lines (125 loc) · 3.82 KB
/
graaljs.js
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
'use strict';
const fetch = require('node-fetch');
const assert = require('assert');
const execa = require('execa');
const Installer = require('../installer');
const { platform, untar, unzip } = require('../common');
function getFilename() {
switch (platform) {
case 'darwin-x64':
return 'macos-amd64';
case 'linux-x64':
return 'linux-amd64';
case 'win32-x64':
return 'windows-amd64';
case 'darwin-arm64':
return 'macos-aarch64';
case 'linux-arm64':
return 'linux-aarch64';
default:
throw new Error(`No GraalJS builds available for ${platform}`);
}
}
function getArchiveExtension() {
return platform.startsWith('win') ? '.zip' : '.tar.gz';
}
class GraalJSVersion {
constructor(tagName) {
const match = /((\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?.*)$/.exec(tagName);
if (match == null) {
throw new Error(`Unable to parse version from tag name '${tagName}'`);
}
const [, fullVersion, ...parts] = match;
this.fullVersion = fullVersion;
this.numParts = parts.filter((s) => s !== undefined).map((s) => parseInt(s, 10));
}
static from(a) {
return a instanceof GraalJSVersion ? a : new GraalJSVersion(a);
}
static compare(a, b) {
a = GraalJSVersion.from(a);
b = GraalJSVersion.from(b);
for (let i = 0; i < Math.max(a.numParts.length, b.numParts.length); i += 1) {
const cmp = Math.sign((a.numParts[i] || 0) - (b.numParts[i] || 0));
if (cmp !== 0) {
return cmp;
}
}
return a.fullVersion.localeCompare(b.fullVersion);
}
toString() {
return this.fullVersion;
}
get majorVersion() {
return this.numParts[0];
}
}
class GraalJSInstaller extends Installer {
constructor(...args) {
super(...args);
this.binPath = undefined;
}
static async resolveVersion(version) {
if (version === 'latest') {
const body = await fetch('https://api.github.com/repos/oracle/graaljs/releases')
.then((r) => r.json());
const versions = body
.filter((b) => !b.prerelease)
.map((b) => GraalJSVersion.from(b.tag_name))
.sort((a, b) => GraalJSVersion.compare(b, a));
if (versions.length === 0) {
throw new Error('Could not find a release version');
}
return versions[0].toString();
}
return version;
}
async getDownloadURL(version) {
return `https://github.com/oracle/graaljs/releases/download/vm-${version}/graaljs-${version}-${getFilename()}${getArchiveExtension()}`;
}
async extract() {
if (platform.startsWith('win')) {
await unzip(this.downloadPath, this.extractedPath);
} else {
await untar(this.downloadPath, this.extractedPath);
}
}
async install() {
const root = `graaljs-${this.version}-${getFilename()}`;
const libjsvm = GraalJSVersion.from(this.version).majorVersion >= 22;
let graaljs;
if (platform.startsWith('darwin')) {
if (libjsvm) {
await this.registerAsset(`${root}/lib/libjsvm.dylib`);
}
graaljs = await this.registerAsset(`${root}/bin/js`);
} else if (platform.startsWith('win')) {
if (libjsvm) {
await this.registerAsset(`${root}/lib/jsvm.dll`);
}
graaljs = await this.registerAsset(`${root}/bin/js.exe`);
} else {
if (libjsvm) {
await this.registerAsset(`${root}/lib/libjsvm.so`);
}
graaljs = await this.registerAsset(`${root}/bin/js`);
}
this.binPath = await this.registerScript('graaljs', `${graaljs}`);
}
async test() {
const program = 'print("42");';
const output = '42';
assert.strictEqual(
(await execa(this.binPath, ['-e', program])).stdout,
output,
);
}
}
GraalJSInstaller.config = {
name: 'GraalJS',
id: 'graaljs',
supported: [
'linux-x64', 'win32-x64', 'darwin-x64', 'linux-arm64', 'darwin-arm64',
],
};
module.exports = GraalJSInstaller;