@@ -43,7 +43,7 @@ Discussion of this proposal is held in [this issue](https://github.com/Kotlin/KE
43
43
* Build scripts (Gradle/Kobalt)
44
44
* Test scripts (Spek)
45
45
* Command-line utilities
46
- * Routing script (ktor)
46
+ * Routing scripts (ktor)
47
47
* Type-safe configuration files (TeamCity)
48
48
* In-process scripting and REPL for IDE
49
49
* Consoles like IPython Notebook
@@ -53,25 +53,38 @@ Discussion of this proposal is held in [this issue](https://github.com/Kotlin/KE
53
53
## Basic definitions
54
54
55
55
- ** 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)
58
58
- ** (Scripting) Host** - an application or a component which handles script execution
59
59
- ** REPL statement** - a group of script text lines, executed in a single REPL eval call
60
60
- ** Compiled script** - a binary compiled code of the script, stored in memory or on disk, which could be loaded
61
61
and instantiated by appropriate platform
62
62
- ** 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
64
66
executed
65
- - ** External script** - another script whose declarations are available for the script being compiled, instantiated and
66
- executed
67
67
- ** Execution environment** - the environment in which the script is instantiated and executed, defining which services,
68
68
objects, actions, etc. are accessible for the script
69
69
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
+
70
83
## Use cases
71
84
72
85
### Embedded scripting
73
86
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,
75
88
IDE and other application-level scripting, etc.
76
89
77
90
#### Environment
@@ -149,39 +162,42 @@ of the compiled script should be supported by the compilation platform.
149
162
#### Execution lifecycle
150
163
151
164
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
157
172
- 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.
159
174
references to external libraries and scripts, compiler options, additional properties that needed to be compiled
160
175
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)*
163
178
- 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
165
180
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 `
167
182
- output: ` scriptInstance `
168
183
- execution - the * Executor* takes the instantiated script and executes the appropriate method, passing arguments
169
184
from the environment to it; this step could be repeated many times
170
- - input: ` scriptInstance `
185
+ - input: ` scriptInstance ` and ` scriptEnvironment `
171
186
- output: script return val
172
187
173
188
#### Processing in an IDE
174
189
175
190
The script is handled in an IDE in the following sequence (* Note that the first two steps are the same as in the
176
191
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
178
193
- * 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
180
196
- * 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
182
198
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,
185
201
navigation, etc) of the script
186
202
187
203
### Standalone scripting
@@ -279,47 +295,16 @@ From an IDE point of view, these scripts are the part of the project.
279
295
280
296
### Architecture
281
297
298
+ #### Components
299
+
282
300
The scripting support consists of the following components:
283
- - ** Platform ** - interface for compilation and IDE support
301
+ - ** ScriptCompiler ** - interface for compilation
284
302
- 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
287
304
- 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
307
306
- 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 `
323
308
- ` (scriptSource) -> compilationConfig `
324
309
- * previously called DependenciesResolver*
325
310
- have access to specific compiler services to:
@@ -333,12 +318,54 @@ The scripting support consists of the following components:
333
318
- gradle's ` buildScript ` and ` plugins ` section handlers
334
319
- ` @DependsOn ` resolver from the ` script-util ` library
335
320
- ...
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
+ - ...
336
335
- ** Selector** - receives the script file and/or text before further analysis to determine whether the script belongs
337
336
to this scripting host and the particular set of components
338
337
- ` (scriptSource) -> Boolean `
339
338
- required for IDE support
340
- - could be combined with the preprocessor, since it may require the same text analyzing services
341
339
- 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)
342
369
343
370
### Script object
344
371
@@ -412,7 +439,7 @@ Alternatively, individual annotations for some elements are possible for specifi
412
439
413
440
### Components interfaces
414
441
415
- #### Compilation Platform
442
+ #### Script Compiler
416
443
417
444
```
418
445
interface ScriptingPlatform<CompilationConfig, ScriptRichEditingConfig> {
0 commit comments