@@ -8,6 +8,13 @@ import {
88 Get ,
99 CreateDataPropertyOrThrow ,
1010 Value ,
11+ Realm ,
12+ Throw ,
13+ Descriptor ,
14+ CreateBuiltinFunction ,
15+ JSStringValue ,
16+ CreateNonEnumerableDataPropertyOrThrow ,
17+ surroundingAgent ,
1118} from '../../lib/engine262.mjs' ;
1219import { Inspector , createConsole } from '../../lib/inspector.mjs' ;
1320import { Test262HarnessFiles } from '../shared/harness.mjs' ;
@@ -44,12 +51,41 @@ function recreateAgent(features, signal) {
4451 const agent = new Agent ( { features } ) ;
4552 setSurroundingAgent ( agent ) ;
4653
54+
4755 inspector . attachAgent ( agent , [ ] ) ;
4856 signal . addEventListener ( 'abort' , ( ) => inspector . detachAgent ( agent ) , { once : true } ) ;
4957
50- const realm = new ManagedRealm ( { } ) ;
58+ const realm = new ManagedRealm ( { name : 'playground repl' } ) ;
5159 createConsole ( realm , { } ) ;
5260
61+ if ( features . includes ( 'virtual-module-loader' ) ) {
62+ const virtualModuleCache = new Map ( ) ;
63+ agent . hostDefinedOptions . loadImportedModule = ( referrer , specifier , attributes , hostDefined , finish ) => {
64+ const importerRealm = referrer instanceof Realm ? referrer : referrer . Realm ;
65+ if ( importerRealm instanceof ManagedRealm && virtualModuleCache . has ( specifier ) ) {
66+ finish ( importerRealm . compileModule ( virtualModuleCache . get ( specifier ) , { specifier } ) ) ;
67+ return ;
68+ }
69+ finish ( Throw ( 'SyntaxError' , 'CouldNotResolveModule' , specifier , 'repl' ) ) ;
70+ }
71+ realm . scope ( ( ) => {
72+ const defineModule = CreateBuiltinFunction . from ( function * defineModule ( specifier , source ) {
73+ if ( ! ( specifier instanceof JSStringValue ) ) {
74+ return Throw ( 'TypeError' , 'NotAString' , specifier ) ;
75+ }
76+ if ( ! ( source instanceof JSStringValue ) ) {
77+ return Throw ( 'TypeError' , 'NotAString' , source ) ;
78+ }
79+ if ( surroundingAgent . debugger_cannotPreview ) {
80+ return surroundingAgent . debugger_cannotPreview ;
81+ }
82+ virtualModuleCache . set ( specifier . stringValue ( ) , source . stringValue ( ) ) ;
83+ return Value . undefined ;
84+ } )
85+ CreateNonEnumerableDataPropertyOrThrow ( realm . GlobalObject , Value ( 'defineModule' ) , defineModule ) ;
86+ } ) ;
87+ }
88+
5389 if ( features . includes ( 'test262-harness' ) ) {
5490 createTest262Intrinsics ( realm , false ) ;
5591 evalQ ( ( _Q , X ) => {
0 commit comments