Skip to content

Commit 61b576c

Browse files
authored
Merge pull request #60 from Mogztter/issue-58-opal-init
2 parents 9bfa3f5 + 1ba8c05 commit 61b576c

File tree

8 files changed

+142
-59
lines changed

8 files changed

+142
-59
lines changed

.github/workflows/build.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Build
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- '0.3.x'
8+
pull_request:
9+
branches:
10+
- '*'
11+
12+
jobs:
13+
build:
14+
strategy:
15+
matrix:
16+
os: [ubuntu-latest]
17+
node-version:
18+
- 12.17
19+
- 14.x
20+
runs-on: ${{ matrix.os }}
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Set up Node ${{ matrix.node-version }}
24+
uses: actions/setup-node@v1
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
- name: Install dependencies
28+
run: |
29+
npm ci
30+
- name: Lint and test
31+
run: |
32+
npm run lint
33+
npm t

.travis.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
},
2323
"main": "src/index.js",
2424
"scripts": {
25-
"test": "mocha spec/*.spec.js",
25+
"test": "mocha spec/*.spec.js && mocha spec/load/load-compat.spec.js && mocha spec/load/load-error.spec.js",
2626
"lint": "standard src/index.js spec npm",
2727
"prepublishOnly": "node npm/prepublish.js",
2828
"postpublish": "node npm/postpublish.js"

spec/load/load-compat.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const chai = require('chai')
2+
const expect = chai.expect
3+
4+
describe('When an compatible version of Opal is loaded', function () {
5+
it('should return the already loaded instance', function () {
6+
// fake an already loaded Opal runtime using version 0.10
7+
globalThis.Opal = {}
8+
globalThis.Opal.$$ = {
9+
'RUBY_ENGINE_VERSION': '0.11'
10+
}
11+
globalThis.Opal.__INSTANCE__ = 'a1b2c3'
12+
const Opal = require('../../src/index').Opal
13+
expect(Opal.$$['RUBY_ENGINE_VERSION']).to.equal('0.11')
14+
expect(Opal.__INSTANCE__).to.equal('a1b2c3')
15+
})
16+
})

spec/load/load-error.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const chai = require('chai')
2+
const expect = chai.expect
3+
4+
describe('When an incompatible version of Opal is loaded', function () {
5+
it('should throw an error', function () {
6+
// fake an already loaded Opal runtime using version 0.10
7+
globalThis.Opal = {}
8+
globalThis.Opal.$$ = {
9+
'RUBY_ENGINE_VERSION': '0.10'
10+
}
11+
expect(() => require('../../src/index').Opal)
12+
.to.throw('Opal is already loaded and version 0.10 is not compatible with 0.11. Please upgrade Asciidoctor.js to the latest version.')
13+
})
14+
})

spec/main.spec.js

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const chai = require('chai');
2-
const expect = chai.expect;
1+
const chai = require('chai')
2+
const expect = chai.expect
33

44
const fundamentalObjects = [
55
Function,
@@ -10,53 +10,62 @@ const fundamentalObjects = [
1010
String,
1111
RegExp,
1212
Array
13-
];
13+
]
1414

1515
// Save the current value of toString() for each object, before the import of Opal
1616
const fundamentalToStringValues = []
1717
for (let index in fundamentalObjects) {
18-
const fundamentalObject = fundamentalObjects[index];
19-
fundamentalToStringValues.push(fundamentalObject.toString());
18+
const fundamentalObject = fundamentalObjects[index]
19+
fundamentalToStringValues.push(fundamentalObject.toString())
2020
}
2121

22-
const Opal = require('../src/index').Opal;
22+
const Opal = require('../src/index').Opal
2323

2424
describe('Opal Node Runtime', function () {
2525

26-
describe('When loaded', function() {
27-
it('should export Opal object', function() {
28-
expect(Opal).not.be.null;
29-
});
26+
describe('When loaded', function () {
27+
it('should export Opal object', function () {
28+
expect(Opal).not.be.null
29+
})
3030

31-
it('should preserve Function methods', function() {
31+
it('should preserve Function methods', function () {
3232

3333
for (let index in fundamentalObjects) {
34-
const fundamentalObject = fundamentalObjects[index];
35-
expect(fundamentalObject.call, `${fundamentalObject.name}.call should be a Function`).to.be.an.instanceof(Function);
36-
expect(fundamentalObject.apply, `${fundamentalObject.name}.apply should be a Function`).to.be.an.instanceof(Function);
37-
expect(fundamentalObject.bind, `${fundamentalObject.name}.bind should be a Function`).to.be.an.instanceof(Function);
38-
expect(fundamentalObject.toString(), `${fundamentalObject.name}.toString should be native function`).to.be.equal(fundamentalToStringValues[index]);
39-
expect(fundamentalObject.toString()).to.equal(`function ${fundamentalObject.name}() { [native code] }`);
34+
const fundamentalObject = fundamentalObjects[index]
35+
expect(fundamentalObject.call, `${fundamentalObject.name}.call should be a Function`).to.be.an.instanceof(Function)
36+
expect(fundamentalObject.apply, `${fundamentalObject.name}.apply should be a Function`).to.be.an.instanceof(Function)
37+
expect(fundamentalObject.bind, `${fundamentalObject.name}.bind should be a Function`).to.be.an.instanceof(Function)
38+
expect(fundamentalObject.toString(), `${fundamentalObject.name}.toString should be native function`).to.be.equal(fundamentalToStringValues[index])
39+
expect(fundamentalObject.toString()).to.equal(`function ${fundamentalObject.name}() { [native code] }`)
4040
}
41-
});
42-
});
43-
44-
describe('When pathname module is loaded', function() {
45-
it('should register Pathname methods', function() {
46-
Opal.load('pathname');
47-
var Pathname = Opal.const_get_relative([], 'Pathname');
48-
var path1 = Pathname.$new('/foo/bar');
49-
var path2 = Pathname.$new('qux');
50-
expect(path1['$+'](path2)['$to_path']()).to.equal('/foo/bar/qux');
51-
});
52-
});
53-
54-
describe('When nodejs module is loaded', function() {
55-
it('should register Node.js specific implementations', function() {
56-
Opal.load('nodejs');
57-
var Dir = Opal.const_get_relative([], 'Dir');
58-
var currentDir = Dir['$pwd']();
59-
expect(currentDir).to.equal(process.cwd());
60-
});
61-
});
62-
});
41+
})
42+
})
43+
44+
describe('When pathname module is loaded', function () {
45+
it('should register Pathname methods', function () {
46+
Opal.load('pathname')
47+
var Pathname = Opal.const_get_relative([], 'Pathname')
48+
var path1 = Pathname.$new('/foo/bar')
49+
var path2 = Pathname.$new('qux')
50+
expect(path1['$+'](path2)['$to_path']()).to.equal('/foo/bar/qux')
51+
})
52+
})
53+
54+
describe('When nodejs module is loaded', function () {
55+
it('should register Node.js specific implementations', function () {
56+
Opal.load('nodejs')
57+
var Dir = Opal.const_get_relative([], 'Dir')
58+
var currentDir = Dir['$pwd']()
59+
expect(currentDir).to.equal(process.cwd())
60+
})
61+
})
62+
63+
describe('When nodejs module is loaded', function () {
64+
it('should register Node.js specific implementations', function () {
65+
Opal.load('nodejs')
66+
var Dir = Opal.const_get_relative([], 'Dir')
67+
var currentDir = Dir['$pwd']()
68+
expect(currentDir).to.equal(process.cwd())
69+
})
70+
})
71+
})

src/index.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
1-
/* global Opal */
2-
require('./opal.js')
3-
require('./nodejs.js')
4-
require('./pathname.js')
5-
require('./stringio.js')
1+
/* global globalThis, self */
2+
var globalObject
3+
if (typeof globalThis !== 'undefined') {
4+
globalObject = globalThis
5+
} else if (typeof self !== 'undefined') {
6+
globalObject = self
7+
} else if (typeof window !== 'undefined') {
8+
globalObject = window
9+
} else if (typeof global !== 'undefined') {
10+
globalObject = global
11+
} else {
12+
throw new Error('Unable to locate global object. Unsupported runtime.')
13+
}
14+
if (globalObject && globalObject.Opal) {
15+
// Opal is already loaded!
16+
var rubyEngineVersion = globalObject.Opal.$$['RUBY_ENGINE_VERSION']
17+
if (!rubyEngineVersion.startsWith('0.11')) {
18+
// incompatible version!
19+
throw new Error('Opal is already loaded and version ' + rubyEngineVersion + ' is not compatible with 0.11. Please upgrade Asciidoctor.js to the latest version.')
20+
}
21+
} else {
22+
// load Opal
23+
require('./opal.js')
24+
require('./nodejs.js')
25+
require('./pathname.js')
26+
require('./stringio.js')
27+
}
628

7-
module.exports.Opal = Opal
29+
module.exports.Opal = globalObject.Opal

src/opal.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
if (!('log' in console)) { console.log = function () {}; }
3232
if (!('warn' in console)) { console.warn = console.log; }
3333

34-
if (typeof(this.Opal) !== 'undefined') {
34+
if (typeof(global_object.Opal) !== 'undefined') {
3535
console.warn('Opal already loaded. Loading twice can cause troubles, please fix your setup.');
36-
return this.Opal;
36+
return global_object.Opal;
3737
}
3838

3939
var nil;

0 commit comments

Comments
 (0)