forked from alibaba/pipcook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
324 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ node_js: | |
- '8' | ||
- '10' | ||
- '12' | ||
- '14' | ||
before_install: | ||
- npm i npminstall -g | ||
install: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import boa from '../lib/index.js'; | ||
const { dir } = boa.builtins(); | ||
const extensionsPrefix = 'py:'; | ||
const protocol = 'nodejs://boa/'; | ||
|
||
export function resolve(specifier, context, defaultResolve) { | ||
if (specifier.startsWith(extensionsPrefix)) { | ||
return { | ||
url: protocol + specifier.replace(extensionsPrefix, '') | ||
}; | ||
} | ||
return defaultResolve(specifier, context, defaultResolve); | ||
} | ||
|
||
export function getFormat(url, context, defaultGetFormat) { | ||
// DynamicInstantiate hook triggered if boa protocol is matched | ||
if (url.startsWith(protocol)) { | ||
return { | ||
format: 'dynamic', | ||
} | ||
} | ||
|
||
// Other protocol are assigned to nodejs for internal judgment loading | ||
return defaultGetFormat(url, context, defaultGetFormat); | ||
} | ||
|
||
export function dynamicInstantiate(url) { | ||
const moduleInstance = boa.import(url.replace(protocol, '')); | ||
// Get all the properties of the Python Object to construct named export | ||
const moduleExports = dir(moduleInstance); | ||
return { | ||
exports: ['default', ...moduleExports], | ||
execute: exports => { | ||
for (let name of moduleExports) { | ||
exports[name].set(moduleInstance[name]); | ||
} | ||
exports.default.set(moduleInstance); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* eslint-disable no-process-exit */ | ||
|
||
'use strict'; | ||
|
||
const path = require('path'); | ||
const { test } = require('tap'); | ||
const { spawnSync } = require('child_process'); | ||
|
||
function getAbsolutePath(relativePath) { | ||
return path.join(__dirname, relativePath); | ||
} | ||
|
||
const FLAG = '--experimental-loader'; | ||
const PATH_ESM_LOADER = getAbsolutePath('../../esm/loader.mjs'); | ||
|
||
// See https://github.com/nodejs/node/pull/29796 | ||
if (process.version < 'v12.11.1') { | ||
console.log(`1..0 # Skipped: Current nodejs version: ${ | ||
process.version} does not support \`--experimental-loader\``); | ||
process.exit(0); | ||
} | ||
|
||
function check(t, appPath) { | ||
const options = { encoding: 'utf8', stdio: 'inherit' }; | ||
const args = ['--no-warnings', FLAG, PATH_ESM_LOADER]; | ||
// See https://github.com/nodejs/node/pull/31974 | ||
if (process.version < 'v14.0.0') { | ||
args.push('--experimental-modules'); | ||
} | ||
args.push(getAbsolutePath(appPath)); | ||
// Running with tap causes errors | ||
// See https://github.com/tapjs/node-tap/issues/673 | ||
// | ||
// The nyc 14 conflicts with the node `--experimental-loader` design, | ||
// which currently uses nyc 15 and tap 14 in combination with a skip error. | ||
const result = spawnSync(process.execPath, args, options); | ||
t.strictEqual(result.signal, null); | ||
t.strictEqual(result.status, 0); | ||
t.end(); | ||
} | ||
|
||
test('python stdlib', t => check(t, './py/test-esm-loader-stdlib.mjs')); | ||
|
||
test('python thirdparty', t => check(t, './py/test-esm-loader-thirdparty.mjs')); | ||
|
||
test('python custom', t => check(t, './py/test-esm-loader-custom.mjs')); | ||
|
||
test('javascript thirdparty', t => check(t, './js/test-esm-loader-thirdparty.mjs')); | ||
|
||
test('javascript custom', t => check(t, './js/test-esm-loader-custom.mjs')); |
7 changes: 7 additions & 0 deletions
7
packages/boa/tests/es-module-loaders/js/test-esm-loader-custom.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { strictEqual } from 'assert'; | ||
import { add, subtract } from './test-esm-loader-math.mjs'; | ||
import math from './test-esm-loader-math.mjs'; | ||
|
||
// custom js | ||
strictEqual(add(1, 2), math.add(1, 2)); | ||
strictEqual(subtract(1, 2), math.subtract(1, 2)); |
8 changes: 8 additions & 0 deletions
8
packages/boa/tests/es-module-loaders/js/test-esm-loader-math.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const math = { | ||
add: (a, b) => a + b, | ||
subtract: (a, b) => a - b | ||
}; | ||
|
||
export const add = math.add; | ||
export const subtract = math.subtract; | ||
export default math; |
4 changes: 4 additions & 0 deletions
4
packages/boa/tests/es-module-loaders/js/test-esm-loader-thirdparty.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import assert from 'assert'; | ||
import tap from 'tap'; | ||
|
||
assert.equal(typeof tap.ok, 'function'); |
34 changes: 34 additions & 0 deletions
34
packages/boa/tests/es-module-loaders/py/test-esm-loader-custom.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { strictEqual, equal } from 'assert'; | ||
import { len } from 'py:builtins'; | ||
import { Foobar } from 'py:tests.base.basic'; | ||
|
||
// define a extended class with basic functions | ||
class Foobar2 extends Foobar { | ||
hellomsg(x) { | ||
return `hello <${x}> on ${this.test}`; | ||
} | ||
} | ||
const f = new Foobar2(); | ||
strictEqual(f.test, 'pythonworld'); | ||
equal(f.ping('rickyes'), 'hello <rickyes> on pythonworld'); | ||
equal(f.callfunc(x => x * 2), 233 * 2); | ||
|
||
// define a class which overloads magic methods | ||
class FoobarList extends Foobar { | ||
constructor() { | ||
super(); | ||
// this is not an es6 array | ||
this.list = [1, 3, 7]; | ||
} | ||
__getitem__(n) { | ||
return this.list[n]; | ||
} | ||
__len__() { | ||
return len(this.list); | ||
} | ||
} | ||
const fl = new FoobarList(); | ||
equal(fl[0], 1); | ||
equal(fl[1], 3); | ||
equal(fl[2], 7); | ||
equal(len(fl), 3); |
71 changes: 71 additions & 0 deletions
71
packages/boa/tests/es-module-loaders/py/test-esm-loader-stdlib.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { strictEqual, ok, equal } from 'assert'; | ||
// export default | ||
import os from 'py:os'; | ||
// named export | ||
import { getenv, getpid } from 'py:os'; | ||
import { rgb_to_hsv } from 'py:colorsys'; | ||
import { getdefaultencoding, dont_write_bytecode } from 'py:sys'; | ||
import { ascii_letters, ascii_lowercase, digits, punctuation } from 'py:string'; | ||
import { | ||
ceil, | ||
copysign, | ||
erf, | ||
floor, | ||
factorial, | ||
gamma, | ||
lgamma, | ||
} from 'py:math'; | ||
import { | ||
abs, | ||
bin, | ||
len, | ||
min, | ||
max, | ||
True, | ||
False, | ||
None, | ||
__debug__ | ||
} from 'py:builtins'; | ||
|
||
// builtins | ||
strictEqual(True, true); | ||
strictEqual(False, false); | ||
strictEqual(None, null); | ||
equal(abs(100), 100); | ||
equal(abs(-100), 100); | ||
equal(bin(3), '0b11'); | ||
ok(__debug__); | ||
|
||
|
||
// os | ||
strictEqual(os.getenv('USER'), getenv('USER')); | ||
strictEqual(getpid() > 0, True); | ||
|
||
// sys | ||
ok(getdefaultencoding() === 'utf-8'); | ||
ok(typeof dont_write_bytecode === 'boolean'); | ||
|
||
// colorsys | ||
const v = rgb_to_hsv(0.2, 0.8, 0.4); | ||
ok(len(v) === 3); | ||
ok(min(v) === 0.3888888888888889); | ||
ok(max(v) === 0.8); | ||
|
||
// math | ||
strictEqual(ceil(10), 10); | ||
strictEqual(copysign(1.0, -1.0), -1); | ||
strictEqual(factorial(8), 40320); | ||
strictEqual(floor(100.99), 100); | ||
ok(erf(0.1)); | ||
strictEqual(gamma(1), 1); | ||
strictEqual(gamma(2), 1); | ||
strictEqual(gamma(3), 2); | ||
ok(lgamma(5)); | ||
|
||
|
||
// string | ||
strictEqual(ascii_letters, | ||
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); | ||
strictEqual(ascii_lowercase, 'abcdefghijklmnopqrstuvwxyz'); | ||
strictEqual(digits, '0123456789'); | ||
strictEqual(punctuation, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'); |
38 changes: 38 additions & 0 deletions
38
packages/boa/tests/es-module-loaders/py/test-esm-loader-thirdparty.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { equal, ok } from 'assert'; | ||
import boa from '../../../lib/index.js'; | ||
import { type, tuple } from 'py:builtins'; | ||
import { | ||
Inf, | ||
NINF, | ||
NAN as NumpyNAN, | ||
array as NumpyArray, | ||
int32 as NumpyInt32, | ||
fv as NumpyFv, | ||
float as NumpyFloat, | ||
} from 'py:numpy'; | ||
import matlib from 'py:numpy.matlib'; | ||
|
||
// numpy constants | ||
equal(Inf, Infinity); | ||
equal(NINF, -Infinity); | ||
ok(isNaN(NumpyNAN, NaN)); | ||
|
||
// numpy array | ||
const x = NumpyArray([[1, 2, 3], [4, 5, 6]], NumpyInt32); | ||
equal(JSON.stringify(x), '[[1,2,3],[4,5,6]]'); | ||
equal(type(x).__name__, 'ndarray'); | ||
equal(x.dtype.name, 'int32', 'the dtype should be int32'); | ||
equal(x.ndim, 2); | ||
equal(x.size, 6); | ||
|
||
// numpy fv | ||
equal(NumpyFv(0.05 / 12, 10 * 12, -100, -100), 15692.928894335748); | ||
|
||
// numpy matlib | ||
const m = matlib.empty(tuple([2, 2])); | ||
equal(type(m).__name__, 'matrix'); | ||
const m2 = matlib.eye(3, boa.kwargs({ | ||
k: 1, | ||
dtype: NumpyFloat, | ||
})); | ||
equal(type(m2).__name__, 'matrix'); |