@@ -33,10 +33,13 @@ import { MRC_STYLE_FUNCTIONS } from '../themes/styles'
33
33
34
34
export const BLOCK_NAME = 'mrc_call_python_function' ;
35
35
36
- const FUNCTION_KIND_MODULE = 'module' ;
37
- const FUNCTION_KIND_STATIC = 'static' ;
38
- const FUNCTION_KIND_CONSTRUCTOR = 'constructor' ;
39
- const FUNCTION_KIND_INSTANCE = 'instance' ;
36
+ enum FunctionKind {
37
+ MODULE = 'module' ,
38
+ STATIC = 'static' ,
39
+ CONSTRUCTOR = 'constructor' ,
40
+ INSTANCE = 'instance' ,
41
+ INSTANCE_WITHIN = 'instance_within' ,
42
+ }
40
43
41
44
const RETURN_TYPE_NONE = 'None' ;
42
45
@@ -47,7 +50,7 @@ export type FunctionArg = {
47
50
48
51
type CallPythonFunctionBlock = Blockly . Block & CallPythonFunctionMixin ;
49
52
interface CallPythonFunctionMixin extends CallPythonFunctionMixinType {
50
- mrcFunctionKind : string , // module, static, constructor, or instance
53
+ mrcFunctionKind : FunctionKind ,
51
54
mrcReturnType : string ,
52
55
mrcArgs : FunctionArg [ ] ,
53
56
mrcTooltip : string ,
@@ -60,7 +63,7 @@ type CallPythonFunctionMixinType = typeof CALL_PYTHON_FUNCTION;
60
63
/** Extra state for serialising call_python_* blocks. */
61
64
type CallPythonFunctionExtraState = {
62
65
/**
63
- * The kind of function: module, static, constructor, or instance .
66
+ * The kind of function. Must be one of the FunctionKind enum values as a string .
64
67
*/
65
68
functionKind : string ,
66
69
/**
@@ -104,31 +107,36 @@ const CALL_PYTHON_FUNCTION = {
104
107
this . setTooltip ( ( ) => {
105
108
let tooltip : string ;
106
109
switch ( this . mrcFunctionKind ) {
107
- case FUNCTION_KIND_MODULE : {
110
+ case FunctionKind . MODULE : {
108
111
const moduleName = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
109
112
const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
110
113
tooltip = 'Calls the function ' + moduleName + '.' + functionName + '.' ;
111
114
break ;
112
115
}
113
- case FUNCTION_KIND_STATIC : {
116
+ case FunctionKind . STATIC : {
114
117
const className = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
115
118
const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
116
119
tooltip = 'Calls the function ' + className + '.' + functionName + '.' ;
117
120
break ;
118
121
}
119
- case FUNCTION_KIND_CONSTRUCTOR : {
122
+ case FunctionKind . CONSTRUCTOR : {
120
123
const className = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
121
124
tooltip = 'Constructs an instance of the class ' + className + '.' ;
122
125
break ;
123
126
}
124
- case FUNCTION_KIND_INSTANCE : {
127
+ case FunctionKind . INSTANCE : {
125
128
const className = this . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
126
129
const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
127
130
tooltip = 'Calls the function ' + className + '.' + functionName + '.' ;
128
131
break ;
129
132
}
133
+ case FunctionKind . INSTANCE_WITHIN : {
134
+ const functionName = this . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
135
+ tooltip = 'Calls the method ' + functionName + '.' ;
136
+ break ;
137
+ }
130
138
default :
131
- throw new Error ( 'mrcVarKind must be "module", "static", "constructor", or "instance".' )
139
+ throw new Error ( 'mrcFunctionKind has unexpected value: ' + mrcFunctionKind )
132
140
}
133
141
const funcTooltip = this . mrcTooltip ;
134
142
if ( funcTooltip ) {
@@ -172,7 +180,7 @@ const CALL_PYTHON_FUNCTION = {
172
180
this : CallPythonFunctionBlock ,
173
181
extraState : CallPythonFunctionExtraState
174
182
) : void {
175
- this . mrcFunctionKind = extraState . functionKind ;
183
+ this . mrcFunctionKind = extraState . functionKind as FunctionKind ;
176
184
this . mrcReturnType = extraState . returnType ;
177
185
this . mrcArgs = [ ] ;
178
186
extraState . args . forEach ( ( arg ) => {
@@ -212,34 +220,39 @@ const CALL_PYTHON_FUNCTION = {
212
220
}
213
221
// Add the dummy input.
214
222
switch ( this . mrcFunctionKind ) {
215
- case FUNCTION_KIND_MODULE :
223
+ case FunctionKind . MODULE :
216
224
this . appendDummyInput ( )
217
225
. appendField ( 'call' )
218
226
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME )
219
227
. appendField ( '.' )
220
228
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
221
229
break ;
222
- case FUNCTION_KIND_STATIC :
230
+ case FunctionKind . STATIC :
223
231
this . appendDummyInput ( )
224
232
. appendField ( 'call' )
225
233
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME )
226
234
. appendField ( '.' )
227
235
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
228
236
break ;
229
- case FUNCTION_KIND_CONSTRUCTOR :
237
+ case FunctionKind . CONSTRUCTOR :
230
238
this . appendDummyInput ( )
231
239
. appendField ( 'create' )
232
240
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
233
241
break ;
234
- case FUNCTION_KIND_INSTANCE :
242
+ case FunctionKind . INSTANCE :
235
243
this . appendDummyInput ( )
236
244
. appendField ( 'call' )
237
245
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_MODULE_OR_CLASS_NAME )
238
246
. appendField ( '.' )
239
247
. appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
240
248
break ;
249
+ case FunctionKind . INSTANCE_WITHIN :
250
+ this . appendDummyInput ( )
251
+ . appendField ( 'call' )
252
+ . appendField ( createFieldNonEditableText ( '' ) , pythonUtils . FIELD_FUNCTION_NAME ) ;
253
+ break ;
241
254
default :
242
- throw new Error ( 'mrcVarKind must be "module", "static", "constructor", or "instance".' )
255
+ throw new Error ( 'mrcFunctionKind has unexpected value: ' + mrcFunctionKind )
243
256
}
244
257
// Add input sockets for the arguments.
245
258
for ( let i = 0 ; i < this . mrcArgs . length ; i ++ ) {
@@ -268,15 +281,15 @@ export const pythonFromBlock = function(
268
281
let code ;
269
282
let argStartIndex = 0 ;
270
283
switch ( callPythonFunctionBlock . mrcFunctionKind ) {
271
- case FUNCTION_KIND_MODULE : {
284
+ case FunctionKind . MODULE : {
272
285
const moduleName = block . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
273
286
const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
274
287
? callPythonFunctionBlock . mrcActualFunctionName
275
288
: block . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
276
289
code = moduleName + '.' + functionName ;
277
290
break ;
278
291
}
279
- case FUNCTION_KIND_STATIC : {
292
+ case FunctionKind . STATIC : {
280
293
const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
281
294
const className = block . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
282
295
const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
@@ -285,13 +298,13 @@ export const pythonFromBlock = function(
285
298
code = className + '.' + functionName ;
286
299
break ;
287
300
}
288
- case FUNCTION_KIND_CONSTRUCTOR : {
301
+ case FunctionKind . CONSTRUCTOR : {
289
302
const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
290
303
const className = block . getFieldValue ( pythonUtils . FIELD_MODULE_OR_CLASS_NAME ) ;
291
304
code = className ;
292
305
break ;
293
306
}
294
- case FUNCTION_KIND_INSTANCE : {
307
+ case FunctionKind . INSTANCE : {
295
308
const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
296
309
const selfValue = generator . valueToCode ( block , 'ARG0' , Order . MEMBER ) ;
297
310
const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
@@ -301,6 +314,16 @@ export const pythonFromBlock = function(
301
314
argStartIndex = 1 ; // Skip the self argument.
302
315
break ;
303
316
}
317
+ case FunctionKind . INSTANCE_WITHIN : {
318
+ const callPythonFunctionBlock = block as CallPythonFunctionBlock ;
319
+ const functionName = ( callPythonFunctionBlock . mrcActualFunctionName )
320
+ ? callPythonFunctionBlock . mrcActualFunctionName
321
+ : block . getFieldValue ( pythonUtils . FIELD_FUNCTION_NAME ) ;
322
+ code = 'self.' + functionName ;
323
+ break ;
324
+ }
325
+ default :
326
+ throw new Error ( 'mrcFunctionKind has unexpected value: ' + mrcFunctionKind )
304
327
}
305
328
code += '(' + generateCodeForArguments ( callPythonFunctionBlock , generator , argStartIndex ) + ')' ;
306
329
if ( block . outputConnection ) {
0 commit comments