File: Kernel.sc

package info (click to toggle)
supercollider 1%3A3.10.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,496 kB
  • sloc: cpp: 283,513; lisp: 74,040; ansic: 72,252; sh: 23,016; python: 7,175; makefile: 1,087; perl: 766; java: 677; yacc: 314; lex: 175; ruby: 136; objc: 65; xml: 15
file content (686 lines) | stat: -rw-r--r-- 18,006 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// you must not make any change at all to the order or number of
// instance variables in these classes!
// You should also not muck with the contents of the instance
// variables unless you are sure you know what you are doing.
// You may add methods.

Class {
	var <name, <nextclass, superclass, <subclasses;
	var <methods, <instVarNames, <classVarNames;
	var <iprototype, <cprototype;
	var <constNames, <constValues;
	var instanceFormat, instanceFlags;
	var <classIndex, classFlags, <maxSubclassIndex;
	var <filenameSymbol, <charPos, <classVarIndex;

	classvar <>classesInited;

	// Every class has a metaclass which has 'Meta_' prepended to the name.
	// Though there is a class Meta_Class which is the class of Class, the
	// class of Meta_Class is Class. It is a loop, but a different one
	// than the structure in Smalltalk.

	superclass {
		// superclass is stored as a symbol to allow forward reference during compilation
		^superclass.asClass
	}
	asClass { ^this }
	isMetaClass { ^this.class === Class }

	initClass {   }


	// call Class.initClassTree(SomeClass) to force a class to init if you depend on its resources
	*initClassTree { arg aClass;
		var implementsInitClass;
		// sometimes you need a class to be inited before another class
		// start the process: Class.initClassTree(Object)
		if(classesInited.isNil, { classesInited = IdentitySet.new });
		if(classesInited.includes(aClass).not, {
			if(aClass.isMetaClass.not and: { aClass.class.findMethod(\initClass).notNil }, {
					aClass.initClass;
			});

			classesInited.add(aClass);
			if(aClass.subclasses.notNil,{
				aClass.subclasses.do({ arg class; this.initClassTree(class); });
			});
		});
	}

	*allClasses {
		_AllClasses
		^this.primitiveFailed
	}
	findMethod { arg methodName;
		if ( methods.notNil, {
			^methods.detect({ arg method; method.name == methodName });
		},{ ^nil });
	}
	findRespondingMethodFor { arg methodName;
		this.superclassesDo { arg class;
			var method = class.findMethod(methodName);
			method !? { ^method };
		};
		^nil
	}
	findOverriddenMethod { arg methodName;
		if(this.findMethod(methodName).isNil) { ^nil };
		this.superclass.superclassesDo { arg class;
			var method = class.findMethod(methodName);
			if(method.notNil) { ^method }
		};
		^nil
	}
	superclassesDo { arg function;
		var class = this;
		while { class.notNil } {
			function.value(class);
			class = class.superclass;
		}
	}

	dumpByteCodes { arg methodName;
		var meth;
		meth = this.findMethod(methodName);
		if (meth.notNil, { meth.dumpByteCodes },{ Post << methodName << " not found.\n"; });
	}

	dumpClassSubtree {
		 _DumpClassSubtree
		^this.primitiveFailed
	}
	dumpInterface {
		// show all methods and their arguments defined for this class
		// does not include methods defined in superclasses
		this.methods.do({ arg meth;
			var numargs;
			numargs = meth.argNames.size - 1;
			"   ".post;
			meth.name.post;
			" ( ".post;
			meth.argNames.do({ arg name, i;
				if (i > 0, { // skip 'this'
					name.post;
					if (i < numargs, {
						", ".post;
					});
				});
			});
			" )\n".post;
		});
	}

	asString {
		^name.asString
	}
	printOn { arg stream;
		stream << "class " << name;
	}
	storeOn { arg stream;
		stream << name;
	}
	archiveAsCompileString { ^true }

	hasHelpFile {
		//should cache this in Library or classvar
		//can't add instance variables to Class
		^this.name.asString.findHelpFile.notNil
	}
	helpFilePath {
		^this.name.asString.findHelpFile
	}
	help {
		this.openHelpFile
	}
	openHelpFile {
		// NOTE: because wslib provided the shortcut "Object:*help --> Object:*openHelpFile", we do the same
		// rather than moving the implementation to the future-compatible :help method.
		// This prevents infinite recursions for people with wslib installed.
		// In future (3.7) this method content should be moved to :help, but no sooner.
		this.name.asString.help
	}

	shallowCopy { ^this }
	//listInstances { _ListInstancesOf }
	//traceAnyPathToAllInstancesOf { _TraceAnyPathToAllInstancesOf }

	openCodeFile {
		this.filenameSymbol.asString.openDocument(this.charPos, -1);
	}
	classVars {
		var start, end;
		start = this.classVarIndex;
		end = start + this.classVarNames.size;
		^thisProcess.instVarAt(0).copyRange(start, end)
	}
	inspectorClass { ^ClassInspector }
	findReferences { arg aSymbol, references;
		methods.do({ arg meth;
			references = meth.findReferences(aSymbol, references)
		});
		^references
	}
	*findAllReferences { arg aSymbol;
		// this will not find method calls that are compiled with special byte codes such as 'value'.
		var references;
		Class.allClasses.do({ arg class;
			references = class.findReferences(aSymbol, references);
		});
		^references;
	}
	allSubclasses {
		var list;
		list = subclasses.copy;
		subclasses.do({ arg class; list = list ++ class.allSubclasses; });
		^list
	}
	superclasses {
		var list;
		this.superclass.superclassesDo { arg class; list = list.add(class) }
		^list
	}

}

Process {
	// A Process is a runtime environment.
	var classVars, <interpreter;
	var curThread, <mainThread;
	var schedulerQueue;
	var <>nowExecutingPath;

	startup {
		var time;

		Class.initClassTree(AppClock); // AppClock first in case of error
		time = this.class.elapsedTime;
		Class.initClassTree(Object);
		("Class tree inited in" + (this.class.elapsedTime - time).round(0.01) + "seconds").postln;
		Class.classesInited = nil;

		topEnvironment = Environment.new;
		currentEnvironment = topEnvironment;
		Archive.read;

		// This method is called automatically right after compiling.
		// Override in class 'Main' to do initialization stuff,
		// but make sure to call this superclass method.

		// the AppClock is not started until after this method is complete
	}
	run {
		// This method is called when 'Run Main' is chosen from the menu.
		// Override in class 'Main' to do whatever you want.
	}
	stop {
		// This method is called when 'Stop Main' is chosen from the menu.
		// Override in class 'Main' to do whatever you want.
	}
	shutdown {
		// This method is called before recompiling or quitting.
		// Override in class 'Main' to do whatever you want.
		ShutDown.run;
		NetAddr.disconnectAll;
		File.closeAll;
		Archive.write;
	}
	tick { // called repeatedly by SCVirtualMachine::doPeriodicTask
		^AppClock.tick;
	}

	*tailCallOptimize {
		_GetTailCallOptimize
		^this.primitiveFailed
	}
	*tailCallOptimize_ { arg bool;
		_SetTailCallOptimize
		^this.primitiveFailed
	}

	getCurrentSelection {
		var qt = \QtGUI.asClass;
		^if(qt.notNil and: {qt.focusView.notNil}) {
			qt.selectedText;
		} {
			interpreter.cmdLine;
		}
	}

	openCodeFile {
		var string, class, method, words;
		string = this.getCurrentSelection;
		if (string.includes($:), {
			string.removeAllSuchThat(_.isSpace);
			words = string.delimit({ arg c; c == $: });
			class = words.at(0).asSymbol.asClass;
			if (class.notNil, {
				method = class.findMethod(words.at(1).asSymbol);
				if (method.notNil, {
					method.filenameSymbol.asString.openDocument(method.charPos, -1);
				});
			});
		},{
			class = string.asSymbol.asClass;
			if (class.notNil, {
				class = class.classRedirect;
				class.filenameSymbol.asString.openDocument(class.charPos, -1);
			});
		});
	}

	openWinCodeFile {
		var string, class, method, words;
		string = this.getCurrentSelection;
		if (string.includes($:), {
			string.removeAllSuchThat(_.isSpace);
			words = string.delimit({ arg c; c == $: });
			class = words.at(0).asSymbol.asClass;
			if (class.notNil, {
				method = class.findMethod(words.at(1).asSymbol);
				if (method.notNil, {
					method.filenameSymbol.asString.openWinTextFile(method.charPos, -1);
				});
			});
		},{
			class = string.asSymbol.asClass;
			if (class.notNil, {
				class = class.classRedirect;
				class.filenameSymbol.asString.openWinTextFile(class.charPos, -1);
			});
		});
	}


	methodReferences {
		// this will not find method calls that are compiled with special byte codes such as 'value'.
		var name, out, references, nameString;
		out = CollStream.new;
		name = this.getCurrentSelection.asSymbol;
		references = Class.findAllReferences(name);
		if (references.notNil, {
			out << "References to '" << name << "' :\n";
			references.do({ arg ref;
				nameString = ref.ownerClass.name ++ ":" ++ ref.name;
				out << "   [" << nameString << "]\n"; });
			out.collection.newTextWindow(name.asString);
		},{
			Post << "\nNo references to '" << name << "'.\n";
		});
	}
	methodTemplates {
		// this constructs the method templates when cmd-Y is pressed in the Lang menu.
		var name, out, found = 0, namestring, text;
		out = CollStream.new;

		text = this.getCurrentSelection;

		if (text.isEmpty){
			Post << "\nNo implementations of ''.\n";
			^this
		};
		if (text[0].toLower != text[0]) {
			// user pressed the wrong key. DWIM.
			^this.openCodeFile;
		};
		name = text.asSymbol;
		out << "Implementations of '" << name << "' :\n";
		Class.allClasses.do({ arg class;
			class.methods.do({ arg method;
				if (method.name == name, {
					found = found + 1;
					namestring = class.name ++ ":" ++ name;
					out << "   [" << namestring << "] :     ";
					if (method.argNames.isNil or: { method.argNames.size == 1 }, {
						out << "this." << name;
						if (name.isSetter, { out << "(val)"; });
					},{
						out << method.argNames.at(0);
						if (name.asString.at(0).isAlpha, {
							out << "." << name << "(";
							method.argNames.do({ arg argName, i;
								if (i > 0, {
									if (i != 1, { out << ", " });
									out << argName;
								});
							});
							out << ")";
						},{
							out << " " << name << " ";
							out << method.argNames.at(1);
						});
					});
					out.nl;
				});
			});
		});
		case
		{ found == 0 }
		{
			Post << "\nNo implementations of '" << name << "'.\n";
		}
		{ found == 1 }
		{
			interpreter.cmdLine = namestring;
			this.openCodeFile;
		}
		{
			out.collection.newTextWindow(name.asString);
		};
	}

	interpretCmdLine {
		// interpret some text from the command line
		interpreter.interpretCmdLine;
	}

	interpretPrintCmdLine {
		// interpret some text from the command line and print result
		interpreter.interpretPrintCmdLine;
	}

	interpretPrintSelectedText {
		interpreter.cmdLine = this.getCurrentSelection;
		interpreter.interpretPrintCmdLine;
	}

	showHelp {
		this.getCurrentSelection.help
	}

	argv { ^[] }

	shallowCopy { ^this }

	*elapsedTime {
		_ElapsedTime
		^this.primitiveFailed
	}

	*monotonicClockTime {
		_monotonicClockTime
		^this.primitiveFailed
	}

	storeOn { arg stream;
		stream << "thisProcess";
	}
	archiveAsCompileString { ^true }

	prSchedulerQueue { ^schedulerQueue }
}


FunctionDef {
	var raw1, raw2, <code, <selectors, <constants, <prototypeFrame, <context, <argNames, <varNames;
	var <sourceCode;

	// a FunctionDef is defined by a code within curly braces {}
	// When you use a FunctionDef in your code it gets pushed on the stack
	// as an instance of Function

	dumpByteCodes {
		_DumpByteCodes
		^this.primitiveFailed
	}

	numArgs {
		// return number of arguments to the function
		_FunDef_NumArgs
		^this.primitiveFailed
	}
	numVars {
		// return number of variables in the function
		_FunDef_NumVars
		^this.primitiveFailed
	}
	varArgs {
		// return boolean whether function has ellipsis argument
		_FunDef_VarArgs
		^this.primitiveFailed
	}
	shallowCopy { ^this }

	asFunction {
		// this is only legal for closed functions.
		_FunctionDefAsFunction
		^this.primitiveFailed
	}

	dumpContexts {
		_FunctionDefDumpContexts
		^this.primitiveFailed
	}
	inspectorClass { ^FunctionDefInspector }

	findReferences { arg aSymbol, references;
		var lits;
		lits = selectors.asArray;
		if (lits.includes(aSymbol), {
			references = references.add(this);
		});
		lits.do({ arg item;
			if (item.isKindOf(FunctionDef), {
				references = item.findReferences(aSymbol, references)
			})
		});
		^references
	}
	storeOn { arg stream;
		stream << "nil"
	}
	checkCanArchive { "cannot archive FunctionDefs".warn }
	archiveAsCompileString { ^true }

	argumentString { arg withDefaultValues=true;
		var res = "", pairs = this.keyValuePairsFromArgs;
		var last;
		if(pairs.isEmpty) { ^nil };
		last = pairs.lastIndex;
		pairs.pairsDo { |name, defaultValue, i|
			var value;
			res = res ++ name;
			if(withDefaultValues and: { defaultValue.notNil }) {
				res = res ++ " = " ++ defaultValue.asCompileString
			};
			if(i + 1 != last) { res = res ++ ", " };
		}
		^res
	}

	keyValuePairsFromArgs {
		var values;
		if(argNames.isNil) { ^[] };
		values = this.prototypeFrame.keep(argNames.size);
		^[argNames, values].flop.flatten
	}

	makeEnvirFromArgs {
		^().putPairs(this.keyValuePairsFromArgs)
	}

}

Method : FunctionDef {
	var <ownerClass, <name, <primitiveName;
	var <filenameSymbol, <charPos;

	openCodeFile {
		this.filenameSymbol.asString.openDocument(this.charPos, -1);
	}
	hasHelpFile {
		//should cache this in Library or classvar
		//can't add instance variables to Class
		^this.name.asString.findHelpFile.notNil
	}
	help {
		HelpBrowser.openHelpForMethod(this);
	}
	inspectorClass { ^MethodInspector }
	storeOn { arg stream;
		stream << ownerClass.name << ".findMethod(" << name.asCompileString << ")"
	}
	archiveAsObject { ^true }
	checkCanArchive {}
	findReferences { arg aSymbol, references;
		var lits, functionRefs;
		lits = selectors.asArray;
		if (lits.includes(aSymbol), {
			references = references.add(this);
			^references // we only need to be listed once
		});
		lits.do({ arg item;
			if (item.isKindOf(FunctionDef), {
				functionRefs = item.findReferences(aSymbol, functionRefs);
			})
		});
		functionRefs.notNil.if({references = references.add(this)});
		^references
	}

	keyValuePairsFromArgs {
		var names, values;
		if(argNames.isNil, { ^[] });
		names = argNames.drop(1); // first argName is "this"
		values = this.prototypeFrame.drop(1).keep(names.size);
		^[names, values].flop.flatten
	}

}

Frame {
	// frames contain the local variables, context and continuation of a function or method invocation.
	// since some Frames are deleted instead of garbage collected, it is too
	// dangerous to allow access to them. Dangling pointers could result.
	shallowCopy { ^this }
	inspectorClass { ^FrameInspector }

	storeOn { arg stream; stream << "nil"; }
	archiveAsCompileString { ^true }
	checkCanArchive { "cannot archive Frames".warn }
}

DebugFrame {
	var <functionDef, <args, <vars, <caller, <context, <address;
	// Object.getBackTrace returns one of these.
	// 'functionDef' is the FunctionDef for this function or method.
	// 'args' the values of the arguments to the function call.
	// 'vars' the values of the local variables.
	// 'caller' points to another DebugFrame for the caller to this function.
	// 'context' points to another DebugFrame for the frame lexically enclosing this one.
	// 'address' memory address of the actual frame object.
	asString { ^"DebugFrame of " ++ functionDef.asString }
}

RawPointer {
	// class used to hold raw pointers from the
	// host environment.
}

Interpreter {
	// The interpreter defines a context in which interactive commands
	// are compiled.

	var <>cmdLine; // place holder for text executed from a worksheet
	var context; // faked interpreter context frame. Don't mess with it.

	// a-z are predefined variables for use by the interactive context.
	// They are read+write so that programmatic methods can
	// get and alter the values that the interpreter has access to.
	var <>a, <>b, <>c, <>d, <>e, <>f, <>g, <>h, <>i, <>j;
	var <>k, <>l, <>m, <>n, <>o, <>p, <>q, <>r, <>s, <>t;
	var <>u, <>v, <>w, <>x, <>y, <>z;

	var <>codeDump, <>preProcessor;

	*new { ^this.shouldNotImplement(thisMethod) }

	interpretCmdLine {
		^this.compile(cmdLine).value;
	}

	interpretPrintCmdLine {
		var res, func, code = cmdLine, doc, ideClass = \ScIDE.asClass;
		preProcessor !? { cmdLine = preProcessor.value(cmdLine, this) };
		func = this.compile(cmdLine);
		if (ideClass.notNil) {
			thisProcess.nowExecutingPath = ideClass.currentPath
		} {
			if(\Document.asClass.notNil and: {(doc = Document.current).tryPerform(\dataptr).notNil}) {
				thisProcess.nowExecutingPath = doc.tryPerform(\path);
			}
		};
		res = func.value;
		thisProcess.nowExecutingPath = nil;
		codeDump.value(code, res, func, this);
		("-> " ++ res).postln;
	}

	interpret { arg string ... args;
		// compile, evaluate
		cmdLine = string;
		^this.compile(string).valueArray(args);
	}
	interpretPrint { arg string ... args;
		// compile, evaluate, print
		cmdLine = string;
		^this.compile(string).valueArray(args).postln;
	}
	compile { arg string;
		_CompileExpression
		// compiles string and returns a Function.
		// the string must be a valid expression.
		// You cannot compile a class definition this way.
		// This method is not implemented in SCPlay.
		^nil
	}

	clearAll {
		a = b = c = d = e = f = g = h = i = j = k = l = m =
		n = o = p = q = r = s = t = u = v = w = x = y = z = nil;
	}

	executeFile { arg pathName ... args;
		var	result, saveExecutingPath = thisProcess.nowExecutingPath;
		if (File.exists(pathName).not) {
			"file \"%\" does not exist.\n".postf(pathName);
			^nil
		};
		thisProcess.nowExecutingPath = pathName;
		protect {
			result = this.compileFile(pathName).valueArray(args)
		} { |exception|
				exception !? { exception.path = pathName };
				thisProcess.nowExecutingPath = saveExecutingPath
		};
		^result
	}

	compileFile { arg pathName;
		var file, text;
		file = File.new(pathName, "r");
		if (file.isOpen.not, {
			error("file open failed\n");
			^nil
		});
		text = file.readAllString;
		file.close;
		preProcessor !? { text = preProcessor.value(text, this) };
		if (text.beginsWith("#!"), {
			// comment out shebang to preserve line count
			text.overWrite("//");
		});
		^this.compile(text)
	}

	// PRIVATE
	functionCompileContext {
		// compiler uses this method as a fake context in which to compile
		// the user's function.
		// Do not edit this method!

		{}	// this forces the compiler to generate a heap allocated frame rather than
			// a frame on the stack
	}
	shallowCopy { ^this }
}