Skip to content

Commit 3d2ebd2

Browse files
committed
Update text after review and prototyping (up to interfaces definitions)
1 parent ba1153c commit 3d2ebd2

File tree

1 file changed

+89
-62
lines changed

1 file changed

+89
-62
lines changed

Diff for: proposals/scripting-support.md

+89-62
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Discussion of this proposal is held in [this issue](https://github.com/Kotlin/KE
4343
* Build scripts (Gradle/Kobalt)
4444
* Test scripts (Spek)
4545
* Command-line utilities
46-
* Routing script (ktor)
46+
* Routing scripts (ktor)
4747
* Type-safe configuration files (TeamCity)
4848
* In-process scripting and REPL for IDE
4949
* Consoles like IPython Notebook
@@ -53,25 +53,38 @@ Discussion of this proposal is held in [this issue](https://github.com/Kotlin/KE
5353
## Basic definitions
5454

5555
- **Script** - a text file written in Kotlin language but allowing top-level statements and having access to some
56-
implicit (not directly mentioned in the script text) properties and , as if the whole script body is a body of an implicit
57-
function
56+
implicit (not directly mentioned in the script text) properties, functions and objects, as if the whole script body
57+
is a body of an implicit function executed in an environment (see below)
5858
- **(Scripting) Host** - an application or a component which handles script execution
5959
- **REPL statement** - a group of script text lines, executed in a single REPL eval call
6060
- **Compiled script** - a binary compiled code of the script, stored in memory or on disk, which could be loaded
6161
and instantiated by appropriate platform
6262
- **Script instance** - instantiated compiled script, ready to be executed
63-
- **External library** - a library whose declarations are available for the script being compiled, instantiated and
63+
- **Dependency** - a external library or other project whose declarations are available for the script being compiled,
64+
instantiated and executed
65+
- **Imported script** - another script whose declarations are available for the script being compiled, instantiated and
6466
executed
65-
- **External script** - another script whose declarations are available for the script being compiled, instantiated and
66-
executed
6767
- **Execution environment** - the environment in which the script is instantiated and executed, defining which services,
6868
objects, actions, etc. are accessible for the script
6969

70+
## feedback
71+
72+
!restrictions! (blacklist/whitelist)
73+
!consent! - run preprocessors in restricted env
74+
!preprocessor extensions! -
75+
!discovery! = only predefined preprocessors
76+
selector: -> Evaluator
77+
executor -> runner
78+
external library -> dependency
79+
external script -> imported script
80+
platform -> compiler adapter
81+
preprocessor -> metadata extractor
82+
7083
## Use cases
7184

7285
### Embedded scripting
7386

74-
Embedded script applications are specialized consoles like Jupyter notebook, Spark shell, embedded games scripting,
87+
Embedded scripting applications are specialized consoles like Jupyter notebook, Spark shell, embedded games scripting,
7588
IDE and other application-level scripting, etc.
7689

7790
#### Environment
@@ -149,39 +162,42 @@ of the compiled script should be supported by the compilation platform.
149162
#### Execution lifecycle
150163

151164
The script is executed in the following pipeline:
152-
- selection - the *Selectors* are called on the script to choose the set of services that will handle it
153-
- input: `scriptSource` (file, url or text with metadata for eval)
154-
- output: preconfigured set of other components (*Preprocessor*, *Platform*, *Executor*, *see below*) that should be
155-
used for further processing of the given script
156-
- preprocessing - the *Preprocessor* takes the script and the environment and extracts the compilation configuration
165+
- selection - the registered *ScriptAnalyzers* are called in the registration order on the script to choose the set of
166+
services that will handle this script
167+
- input: `scriptSource` (file, url or text)
168+
- output: preconfigured set of other components (*ScriptAnalyzer*, *ScriptCompiler*, *ScriptRunner* and/or
169+
*ScriptingHost* *, *see below*) that should be used for further processing of the given script
170+
- metadata extraction - the *ScriptAnalyzer* takes the script and the environment and extracts the script metadata
171+
required for the compilation
157172
- input: `scriptSource` *(see selection)*
158-
- output: `compilationConfig` - data needed for proper script compilation, defined by platform; may include, e.g.
173+
- output: `scriptMetadata` - data needed for proper script compilation, defined by platform; may include, e.g.
159174
references to external libraries and scripts, compiler options, additional properties that needed to be compiled
160175
into the script class, etc.
161-
- compilation - the *Platform* takes the script, and the compilation configuration and provides the compiled class
162-
- input: `scriptSource` and `compilationConfig` *(see above)*
176+
- compilation - the *ScriptCompiler* takes the script, and the `scriptMetadata` and provides the compiled class
177+
- input: `scriptSource` and `scriptMetadata` *(see above)*
163178
- output: `compiledScript` - a compiled script representation, e.g. class file or binary class data for JVM
164-
- instantiation - the *Executor* takes the compiled class, the environment, and the compilation configuration and
179+
- instantiation - the *ScriptRunner* takes the compiled class, the environment, and the compilation configuration and
165180
instantiates the script class, passing the required parameters from the environment to the constructor
166-
- input: `compiledScript` *(see above)*
181+
- input: `compiledScript` *(see above)* and `scriptEnvironment`
167182
- output: `scriptInstance`
168183
- execution - the *Executor* takes the instantiated script and executes the appropriate method, passing arguments
169184
from the environment to it; this step could be repeated many times
170-
- input: `scriptInstance`
185+
- input: `scriptInstance` and `scriptEnvironment`
171186
- output: script return val
172187

173188
#### Processing in an IDE
174189

175190
The script is handled in an IDE in the following sequence (*Note that the first two steps are the same as in the
176191
execution pipeline*):
177-
- selection - the *Selectors* takes the script and selects the set of services that will handle it
192+
- selection - the *ScriptAnalyzers* takes the script and selects the set of services that will handle it
178193
- *input and output are the same as in the execution lifecycle*
179-
- preprocessing - the *Preprocessor* takes the script and the environment and extracts the compilation configuration
194+
- metadata extraction - the *ScriptAnalyzer* takes the script and the environment and extracts the script metadata
195+
required for the ide resolving
180196
- *input and output are the same as in the execution lifecycle*
181-
- resolution info extraction - the *Platform* takes the script and the compilation configuration and provides the
197+
- resolution info extraction - the *IDE resolver* takes the script and the compilation configuration and provides the
182198
info required for rich editing the script
183-
- input: `scriptSource` and `compilationConfig` *(see above)*
184-
- output: `scriptRichEditingConfig` - the data that will enable rich editing (resolving, source highlighting,
199+
- input: `scriptSource` and `scriptMetadata` *(see above)*
200+
- output: `scriptResolveData` - the data that will enable rich editing (resolving, source highlighting,
185201
navigation, etc) of the script
186202

187203
### Standalone scripting
@@ -279,47 +295,16 @@ From an IDE point of view, these scripts are the part of the project.
279295

280296
### Architecture
281297

298+
#### Components
299+
282300
The scripting support consists of the following components:
283-
- **Platform** - interface for compilation and IDE support
301+
- **ScriptCompiler** - interface for compilation
284302
- compilation: `(scriptSource, scriptDefinition, compilationConfig) -> compiledScript`
285-
- IDE resolving: `(scriptSource, scriptDefinition, compilationConfig) -> scriptRichEditingConfig`
286-
- predefined platforms based on the kotlin platforms: /JVM, /JS, /Native
303+
- predefined script compilers based on the kotlin platforms: /JVM, /JS, /Native
287304
- custom/customized implementation possible
288-
- compiled scripts cashing should be implemented here
289-
- the `scriptSource` is defined by the platform and determines the way to access script for other components; it
290-
consists of:
291-
- the script reference pointer: path or url
292-
- an accessor to the script text
293-
- accessors to the syntactic script elements that could be used on the preprocessing, such as:
294-
- file-level annotations
295-
- "sections": top-level `name {...}` blocks (e.g. as provided by `source-sections-plugin` and used in gradle)
296-
- *(maybe, in the future)* lexeme iterators
297-
- the `scriptDefinition` is a script type configuration in a form of annotated class/interface; *see dedicated section
298-
below for details*
299-
- the `compilationConfig` is defined by the platform, and may affect code generation; typically contains:
300-
- additional dependencies
301-
- *required* scripts
302-
- expected parameters
303-
- the `scriptRichEditingConfig` is defined by the platform too but is specific for a particular IDE; it may contain:
304-
- data from `compilationConfig` that references script dependencies and implicit declarations (like expected parameters)
305-
- additional dependencies data, like source and javadoc jars
306-
- platform configuration data (e.g. JAVA_HOME)
305+
- compiled scripts cashing belongs here
307306
- should not keep the state of the script execution
308-
- **Executor** - the component that receives compiled script instantiates it, and then evaluates it in a required
309-
environment, supplying any arguments that the script requires:
310-
- instantiation: `(compiledScript, compilationConfig, environment) -> scriptInstance`
311-
- evaluation: `(scriptInstance, compilationConfig, environment) -> Unit`
312-
- the `environment` is an entity denoting/referencing the actual execution environment of the script
313-
- uses `compilationConfig` and `environment` to compute arguments for the script
314-
- predefined platform-specific executors available, but could be provided by the scripting host
315-
- possible executors
316-
- JSR-223
317-
- IDEA REPL
318-
- Jupyter
319-
- Gradle
320-
- with specific coroutines context
321-
- ...
322-
- **Preprocessor** - receives the script text before compilation and extracts the `compilationConfig`
307+
- **ScriptAnalyzer** - recognizes the script by the locator or contents and extracts the `scriptMetadata`
323308
- `(scriptSource) -> compilationConfig`
324309
- *previously called DependenciesResolver*
325310
- have access to specific compiler services to:
@@ -333,12 +318,54 @@ The scripting support consists of the following components:
333318
- gradle's `buildScript` and `plugins` section handlers
334319
- `@DependsOn` resolver from the `script-util` library
335320
- ...
321+
- **ScriptRunner** - the component that receives compiled script instantiates it, and then evaluates it in a required
322+
environment, supplying any arguments that the script requires:
323+
- instantiation: `(compiledScript, scriptMetadata, environment) -> scriptInstance`
324+
- evaluation: `(scriptInstance, scriptMetadata, environment) -> Unit`
325+
- the `environment` is an entity denoting/referencing the actual execution environment of the script
326+
- uses `scriptMetadata` and `environment` to compute arguments for the script
327+
- predefined platform-specific executors available, but could be provided by the scripting host
328+
- possible executors
329+
- JSR-223
330+
- IDEA REPL
331+
- Jupyter
332+
- Gradle
333+
- with specific coroutines context
334+
- ...
336335
- **Selector** - receives the script file and/or text before further analysis to determine whether the script belongs
337336
to this scripting host and the particular set of components
338337
- `(scriptSource) -> Boolean`
339338
- required for IDE support
340-
- could be combined with the preprocessor, since it may require the same text analyzing services
341339
- default and simple implementations are provided
340+
- **IDE support** - interface similar to **ScriptCompiler** for providing IDEs with data for rich editing
341+
- IDE resolving: `(scriptSource, scriptMetadata) -> scriptResolveData`
342+
343+
#### Data structures
344+
345+
- the `scriptSource` determines the way to access script for other components; it
346+
consists of:
347+
- the script reference pointer: path or url
348+
- an accessor to the script text
349+
- the `scriptMetadata` contains the info required for script compilation and resolving, including
350+
- accessors to the syntactic script elements that could be used on the preprocessing, such as:
351+
- file-level annotations
352+
- "sections": top-level `name {...}` blocks (e.g. as provided by `source-sections-plugin` and used in gradle)
353+
- *(maybe, in the future)* lexeme iterators
354+
- `scriptDefinition` class used as a prototype for code generation (*see below*)
355+
- script dependencies
356+
- implicit imports
357+
- compiler options
358+
- implicit receivers, e.g. previous script lines
359+
- external bindings
360+
- additional compiler options
361+
- imported scripts
362+
- expected parameters
363+
- API restrictions in the form of enable/disable rules list, each rule contains e.g. qualified name or a wildcard
364+
- the `scriptDefinition` is a script type configuration in a form of annotated class/interface; *see dedicated section
365+
below for details*
366+
- the `scriptResolveData` is specific for a particular IDE; it may contain:
367+
- additional dependencies data, like source and javadoc jars
368+
- platform configuration data (e.g. JAVA_HOME)
342369

343370
### Script object
344371

@@ -412,7 +439,7 @@ Alternatively, individual annotations for some elements are possible for specifi
412439

413440
### Components interfaces
414441

415-
#### Compilation Platform
442+
#### Script Compiler
416443

417444
```
418445
interface ScriptingPlatform<CompilationConfig, ScriptRichEditingConfig> {

0 commit comments

Comments
 (0)