Skip to content
Snippets Groups Projects
Commit b0ec060e authored by Leo Balter's avatar Leo Balter
Browse files

Add more cases for Dynamic Imports usage

- Add cases for mixing module and script code
- Rename test case from return promise to thenable
- Fix script code case with valid loaded fixture
- Add a test to assert a promise return
- Add case for specifier toString rejection
- Add case for specifier toString
- Test Assignment expression abrupt completion
- Test Promise return
parent e1ff0bbe
No related branches found
No related tags found
No related merge requests found
Showing
with 225 additions and 2 deletions
// Copyright (C) 2018 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
desc: import() from a ascript code can load a file with module code
template: default
---*/
//- setup
// This is still valid in script code, and should not be valid for module code
// https://tc39.github.io/ecma262/#sec-scripts-static-semantics-lexicallydeclarednames
var smoosh; function smoosh() {}
//- import
import('./module-code_FIXTURE.js')
//- body
assert.sameValue(imported.default, 42);
assert.sameValue(imported.x, 'Test262');
assert.sameValue(imported.z, 42);
// Copyright (C) 2018 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
desc: >
import() from a module code can load a file with script code, but the target
is resolved into a Module Record
info: |
Modules
Static Semantics: Early Errors
ModuleBody : ModuleItemList
- It is a Syntax Error if the LexicallyDeclaredNames of ModuleItemList containsany duplicate entries.
- It is a Syntax Error if any element of the LexicallyDeclaredNames of ModuleItemList also occurs in the VarDeclaredNames of ModuleItemList.
template: catch
flags: [module]
---*/
//- import
import('./script-code_FIXTURE.js')
//- body
assert.sameValue(error.name, 'SyntaxError');
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Copyright (C) 2018 the V8 project authors. All rights reserved. // Copyright (C) 2018 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file. // This code is governed by the BSD license found in the LICENSE file.
/*--- /*---
desc: Dynamic import() returns a Promise object. desc: Dynamic import() returns a thenable object.
template: default template: default
---*/ ---*/
......
...@@ -11,4 +11,4 @@ template: syntax/valid ...@@ -11,4 +11,4 @@ template: syntax/valid
var smoosh; function smoosh() {} var smoosh; function smoosh() {}
//- import //- import
import('./script-code-valid.js') import('./empty_FIXTURE.js')
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// Copyright (C) 2018 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
desc: >
Abrupt from ToString(specifier) rejects the promise
esid: sec-moduleevaluation
info: |
Import Calls
Runtime Semantics: Evaluation
ImportCall : import(AssignmentExpression)
1. Let referencingScriptOrModule be ! GetActiveScriptOrModule().
2. Let argRef be the result of evaluating AssignmentExpression.
3. Let specifier be ? GetValue(argRef).
4. Let promiseCapability be ! NewPromiseCapability(%Promise%).
5. Let specifierString be ToString(specifier).
6. IfAbruptRejectPromise(specifierString, promiseCapability).
7. Perform ! HostImportModuleDynamically(referencingScriptOrModule, specifierString, promiseCapability).
8. Return promiseCapability.[[Promise]].
template: catch
---*/
//- setup
const obj = {
toString() {
throw 'custom error';
}
};
//- import
import(obj)
//- body
assert.sameValue(error, 'custom error');
// Copyright (C) 2018 Rick Waldron. All rights reserved.
// Copyright (C) 2018 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
desc: >
ToString value of specifier
esid: sec-moduleevaluation
info: |
Import Calls
Runtime Semantics: Evaluation
ImportCall : import(AssignmentExpression)
1. Let referencingScriptOrModule be ! GetActiveScriptOrModule().
2. Let argRef be the result of evaluating AssignmentExpression.
3. Let specifier be ? GetValue(argRef).
4. Let promiseCapability be ! NewPromiseCapability(%Promise%).
5. Let specifierString be ToString(specifier).
6. IfAbruptRejectPromise(specifierString, promiseCapability).
7. Perform ! HostImportModuleDynamically(referencingScriptOrModule, specifierString, promiseCapability).
8. Return promiseCapability.[[Promise]].
template: default
---*/
//- setup
const obj = {
toString() {
return './module-code_FIXTURE.js';
}
};
//- import
import(obj)
//- body
assert.sameValue(imported.default, 42);
assert.sameValue(imported.x, 'Test262');
assert.sameValue(imported.z, 42);
// Copyright (C) 2018 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Return Abrupt from the GetValue evaluation on the given AssignmentExpression
esid: sec-import-call-runtime-semantics-evaluation
info: |
Import Calls
Runtime Semantics: Evaluation
ImportCall : import(AssignmentExpression)
1. Let referencingScriptOrModule be ! GetActiveScriptOrModule().
2. Let argRef be the result of evaluating AssignmentExpression.
3. Let specifier be ? GetValue(argRef).
4. Let promiseCapability be ! NewPromiseCapability(%Promise%).
5. Let specifierString be ToString(specifier).
6. IfAbruptRejectPromise(specifierString, promiseCapability).
7. Perform ! HostImportModuleDynamically(referencingScriptOrModule, specifierString, promiseCapability).
8. Return promiseCapability.[[Promise]].
features: [dynamic-import]
---*/
const obj = {
get err() {
throw new Test262Error('catpure this on evaluation')
}
}
assert.throws(Test262Error, function() {
import(obj.err);
}, 'Custom Error getting property value');
assert.throws(ReferenceError, function() {
import(refErr);
}, 'bad reference');
// Copyright (C) 2018 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
// This is still valid in script code and strict and non strict modes
// This is valid as module code
// https://tc39.github.io/ecma262/#sec-scripts-static-semantics-lexicallydeclarednames
var smoosh; function smoosh() {}
// Copyright (C) 2018 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
export var x = 1;
// Copyright (C) 2018 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
ImportCall returns a promise
esid: sec-import-call-runtime-semantics-evaluation
info: |
Import Calls
Runtime Semantics: Evaluation
ImportCall : import(AssignmentExpression)
1. Let referencingScriptOrModule be ! GetActiveScriptOrModule().
2. Let argRef be the result of evaluating AssignmentExpression.
3. Let specifier be ? GetValue(argRef).
4. Let promiseCapability be ! NewPromiseCapability(%Promise%).
5. Let specifierString be ToString(specifier).
6. IfAbruptRejectPromise(specifierString, promiseCapability).
7. Perform ! HostImportModuleDynamically(referencingScriptOrModule, specifierString, promiseCapability).
8. Return promiseCapability.[[Promise]].
features: [dynamic-import]
includes: [fnGlobalObject.js]
---*/
const originalPromise = Promise;
fnGlobalObject().Promise = function() {
throw "This should not be called";
};
const p = import('./dynamic-import-module_FIXTURE.js');
assert.sameValue(p.constructor, originalPromise, 'constructor is %Promise%');
assert.sameValue(Object.getPrototypeOf(p), originalPromise.prototype, 'prototype is %PromisePrototype%');
assert.sameValue(p.then, originalPromise.prototype.then, 'preserves the original `then` method');
assert.sameValue(p.catch, originalPromise.prototype.catch, 'preserves the original `catch` method');
assert.sameValue(p.finally, originalPromise.prototype.finally, 'preserves the original `finally` method');
assert.sameValue(
Object.prototype.hasOwnProperty.call(p, 'then'), false,
'returned promise has no own property then'
);
assert.sameValue(
Object.prototype.hasOwnProperty.call(p, 'catch'), false,
'returned promise has no own property catch'
);
assert.sameValue(
Object.prototype.hasOwnProperty.call(p, 'finally'), false,
'returned promise has no own property finally'
);
// Copyright (C) 2018 Leo Balter. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
const x = 'Test262';
const y = 42;
export default y;
export { x, y as z };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment