1 title:: Writing Primitives
2 summary:: Writing Primitives
6 section:: SuperCollider code
10 prGetPathsDialog { arg returnSlot;
17 section:: Primitive source code
19 In your primitive source code define the primitive:
22 void initCocoaFilePrimitives()
26 base = nextPrimitiveIndex();
29 definePrimitive(base, index++, "_Cocoa_GetPathsDialog", prGetPathsDialog, 2, 0);
30 // further primitives can be laid in...
31 //definePrimitive(base, index++, "_Cocoa_SaveAsPlist", prSaveAsPlist, 3, 0);
36 Here is the prototype for definePrimitive:
39 int definePrimitive(int base, int index, char *name, PrimitiveHandler handler, int numArgs, int varArgs);
42 The numArgs is the number of arguments that were passed into the SuperCollider method that calls the primitive, plus one
43 to include the receiver which is passed in as the first argument.
47 section:: Write your primitive
49 teletype::g->sp:: is the top of the stack and is the last argument pushed.
50 teletype::g->sp - inNumArgsPushed + 1 :: is the receiver and where the result goes.
52 In this example, the numArgsPushed will be 2 (as specified in definePrimitive)
55 int prGetPathsDialog(struct VMGlobals *g, int numArgsPushed)
57 if (!g->canCallOS) return errCantCallOS; //if its deferred, does this matter ?
59 PyrSlot *receiver = g->sp - 1; // an instance of Cocoa
60 PyrSlot *array = g->sp; // an array
68 This example does not set the receiver, so the primitive returns the original receiver unchanged (still an instance of
69 Cocoa). Or set the object at teletype::receiver:: which again is at teletype::(g->sp - numArgsPushed + 1)::.
74 If possible, you should avoid creating objects in a primitive. Primitives are much simpler to write and debug if you
75 pass in an object that you create in SC code and fill in its slots in the primitive.
77 When you do fill in slots in an object with other objects, you must call teletype::g->gc->GCWrite(obj, slot):: in order
78 to notify the garbage collector that you have modified a slot that it may have already scanned.
80 warning::Do not store pointers to PyrObjects in C/C++ variables unless you can absolutely guarantee that they cannot be garbage
81 collected. For example the File and SCWindow classes do this by storing the objects in an array in a classvar. The
82 object has to stay in that array until no C object refers to it.
83 strong::Failing to observe the above two points can result in very hard to find bugs.::
86 If you create more than one object in a primitive you must make sure that all the previously created objects are
87 reachable before you allocate another. In other words you must store them on the stack or in another object's slots
88 before creating another. Creating objects can call the garbage collector and if you have not made your objects
89 reachable, they can get collected out from under you.
91 Since SC is dynamically typed, you cannot rely on any of the arguments being of the class you expect. You should check
92 every argument to make sure it is the correct type. One way to do this is by using teletype::isKindOfSlot::. If you just
93 want a numeric value, you can use teletype::slotIntVal::, teletype::slotFloatVal::, or teletype::slotDoubleVal:: which
94 will return an error if the value is not a numeric type. Similarly there is teletype::slotStringVal::. It is safe to
95 assume that the receiver will be of the correct type because this is ensured by the method dispatch mechanism.
100 ## Now where do i put the thing to return it?
101 || into teletype::g->sp - inNumArgsPushed + 1 :: (In most primitives this is referred to by the variable teletype::a::).