ReferenceManual.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. The Skulpt Reference Manual
  2. ---------------------------
  3. It would take a bit of research and work but I also wonder if there is not a subset of other skulpt functions that could be collected together more nicely as the skulpt internal api.
  4. This could or should probably more closely follow the C API defined by CPython. https://docs.python.org/2/c-api/index.html
  5. Although the more I think about it the more I think we just need to do a better job of providing some organized documentation. I've started an outline, and I think if we can get a decent outline and agree on some general principles for development we could actually document Skulpt and make it much more accessible for people to help.
  6. - Terminology
  7. - "Python objects" versus "Javascript objects"
  8. - Sk.builtin
  9. - Sk.misceval
  10. - Sk.ffi
  11. - Sk.abstr
  12. - slot functions
  13. - builtin functions
  14. - magic methods
  15. - Standard Data Type Interfaces
  16. - Checking types
  17. - Checking argument types Sk.builtin.pyCheckType -- weird function that takes one of the below as an argument
  18. - Sk.builtin.checkNumber
  19. - Sk.builtin.checkComplex
  20. - Sk.builtin.checkInt
  21. - Sk.builtin.checkString
  22. - Sk.builtin.checkClass
  23. - Sk.builtin.checkBool
  24. - Sk.builtin.checkNone
  25. - Sk.builtin.checkFunction
  26. - Sk.builtin.checkIterable
  27. - Sk.builtin.checkCallable
  28. - Sk.builtin.checkSequence
  29. - Common
  30. - Determining if an object is iterable
  31. - General Sequence and slicing operators (many Sk.abstr functions)
  32. - lists
  33. - dictionaries
  34. - sets
  35. - integers/longs
  36. - floats
  37. - complex
  38. - boolean
  39. - Exceptions
  40. - None
  41. - Operators
  42. - binary operators
  43. - unary
  44. - Dunder methods
  45. - the builtin names tp$xxx, nb$xxx, sq$xxxx
  46. - mapping to the __ names
  47. - creating a class
  48. - building a class Sk.misceval.buildClass
  49. - Iteration
  50. - Getting an interator
  51. - iterating with an iterator
  52. - Comparing
  53. - richCompareBool
  54. - isTrue
  55. - functions — callable from Python
  56. - The function wrapper Sk.builtin.func -- meant to be called ``new Sk.builtin.func(javascriptfunc)``
  57. - calling a Python function from Javascript
  58. - named arguments
  59. - *args
  60. - **kwargs
  61. - creating a module
  62. - module template
  63. - exposing functions from the module
  64. - exposing constants
  65. - creating classes in a module
  66. - To Javascript and Back to Python
  67. - The Sk.ffi interface
  68. - Utility functions
  69. - Checking argument counts Sk.builtin.pyCheckArgs
  70. - Importing and Running code
  71. - Running source from a string
  72. - importing a module/package
  73. -
  74. - Functions for working with Python Objects
  75. The abstr namespace defines a bunch of functions that you should use when working with Python objects.
  76. - Sk.abstr.typeName = function (v)
  77. - Sk.abstr.binop_type_error = function (v, w, name)
  78. - Sk.abstr.unop_type_error = function (v, name)
  79. - Sk.abstr.boNameToSlotFuncLhs_ = function (obj, name)
  80. - Sk.abstr.boNameToSlotFuncRhs_ = function (obj, name)
  81. - Sk.abstr.iboNameToSlotFunc_ = function (obj, name)
  82. - Sk.abstr.uoNameToSlotFunc_ = function (obj, name)
  83. - Sk.abstr.binary_op_ = function (v, w, opname)
  84. - Sk.abstr.binary_iop_ = function (v, w, opname)
  85. - Sk.abstr.unary_op_ = function (v, opname)
  86. - Sk.abstr.numOpAndPromote = function (a, b, opfn)
  87. - Sk.abstr.boNumPromote_ =
  88. - Sk.abstr.numberBinOp = function (v, w, op)
  89. - Sk.abstr.numberInplaceBinOp = function (v, w, op)
  90. - Sk.abstr.numberUnaryOp = function (v, op)
  91. - Sk.abstr.fixSeqIndex_ = function (seq, i)
  92. - Sk.abstr.sequenceContains = function (seq, ob)
  93. - Sk.abstr.sequenceConcat = function (seq1, seq2)
  94. - Sk.abstr.sequenceGetIndexOf = function (seq, ob)
  95. - Sk.abstr.sequenceGetCountOf = function (seq, ob)
  96. - Sk.abstr.sequenceGetItem = function (seq, i, canSuspend)
  97. - Sk.abstr.sequenceSetItem = function (seq, i, x, canSuspend)
  98. - Sk.abstr.sequenceDelItem = function (seq, i)
  99. - Sk.abstr.sequenceRepeat = function (f, seq, n)
  100. - Sk.abstr.sequenceGetSlice = function (seq, i1, i2)
  101. - Sk.abstr.sequenceDelSlice = function (seq, i1, i2)
  102. - Sk.abstr.sequenceSetSlice = function (seq, i1, i2, x)
  103. - Sk.abstr.sequenceUnpack = function (seq, n)
  104. - Sk.abstr.objectFormat = function (obj, format_spec)
  105. - Sk.abstr.objectAdd = function (a, b)
  106. - Sk.abstr.objectNegative = function (obj)
  107. - Sk.abstr.objectPositive = function (obj)
  108. - Sk.abstr.objectDelItem = function (o, key)
  109. - Sk.abstr.objectGetItem = function (o, key, canSuspend)
  110. - Sk.abstr.objectSetItem = function (o, key, v, canSuspend)
  111. - Sk.abstr.gattr = function (obj, nameJS, canSuspend)
  112. - Sk.abstr.sattr = function (obj, nameJS, data, canSuspend)
  113. - Sk.abstr.iter = function (obj)
  114. - Sk.abstr.iternext = function (it, canSuspend)
  115. Sk.misceval
  116. Misc
  117. * Sk.misceval.Suspension = function Suspension(resume, child, data)
  118. * Sk.misceval.retryOptionalSuspensionOrThrow = function (susp, message)
  119. * Sk.misceval.isIndex = function (o)
  120. * Sk.misceval.asIndex = function (o)
  121. * Sk.misceval.applySlice = function (u, v, w, canSuspend)
  122. * Sk.misceval.assignSlice = function (u, v, w, x, canSuspend)
  123. * Sk.misceval.arrayFromArguments = function (args)
  124. * Sk.misceval.swappedOp_ =
  125. * Sk.misceval.richCompareBool = function (v, w, op)
  126. * Sk.misceval.objectRepr = function (v)
  127. * Sk.misceval.opAllowsEquality = function (op)
  128. * Sk.misceval.isTrue = function (x)
  129. * Sk.misceval.softspace_ = fals
  130. * Sk.misceval.print_ = function (x)
  131. * Sk.misceval.loadname = function (name, other)
  132. * Sk.misceval.call = function (func, kwdict, varargseq, kws, args)
  133. * Sk.misceval.callAsync = function (suspensionHandlers, func, kwdict, varargseq, kws, args)
  134. * Sk.misceval.callOrSuspend = function (func, kwdict, varargseq, kws, args)
  135. * Sk.misceval.callsim = function (func, args)
  136. * Sk.misceval.callsimAsync = function (suspensionHandlers, func, args)
  137. * Sk.misceval.callsimOrSuspend = function (func, args)
  138. * Sk.misceval.apply = function (func, kwdict, varargseq, kws, args)
  139. * Sk.misceval.asyncToPromise = function(suspendablefn, suspHandlers)
  140. * Sk.misceval.applyAsync = function (suspHandlers, func, kwdict, varargseq, kws, args)
  141. * Sk.misceval.chain = function (initialValue, chainedFns)
  142. * Sk.misceval.applyOrSuspend = function (func, kwdict, varargseq, kws, args)
  143. * Sk.misceval.buildClass = function (globals, func, name, bases)
  144. - Understanding the mapping from Skupt api functions to dunder methods
  145. .. code-block:: python
  146. SLOTS = [
  147. # typeobject
  148. Slot("__new__", "tp$new", "new"),
  149. Slot("__init__", "tp$init", "init"),
  150. Slot("__str__", "tp$print", "print"),
  151. Slot("__repr__", "tp$repr", "repr",
  152. opcode="UNARY_CONVERT"),
  153. Slot("__hash__", "tp$hash", "hash"),
  154. Slot("__call__", "tp$call", "call"),
  155. # Note: In CPython, if tp$getattro exists, tp$getattr is never called.
  156. Slot("__getattribute__", "tp$getattro", "getattro"),
  157. Slot("__getattr__", "tp$getattro", "getattro"),
  158. Slot("__setattr__", "tp$setattro", "setattro"),
  159. Slot("__delattr__", "tp$setattro", "setattro"),
  160. # for Py_TPFLAGS_HAVE_ITER:
  161. Slot("__iter__", "tp$iter", "unary"),
  162. Slot("next", "tp$iternext", "next", python_version="2"),
  163. Slot("__next__", "tp$iternext", "next", python_version="3"),
  164. # for Py_TPFLAGS_HAVE_CLASS:
  165. Slot("__get__", "tp$descr_get", "descr_get"),
  166. Slot("__set__", "tp$descr_set", "descr_set"),
  167. Slot("__delete__", "tp$descr_set", "descr_delete"),
  168. Slot("__del__", "tp$del", "destructor"),
  169. # all typically done by __richcompare__
  170. Slot("__cmp$_", "tp$compare", "cmp",
  171. python_version="2"), # "tp$reserved" in Python 3
  172. Slot("__lt__", "tp$richcompare", "richcmpfunc"),
  173. Slot("__le__", "tp$richcompare", "richcmpfunc"),
  174. Slot("__eq__", "tp$richcompare", "richcmpfunc"),
  175. Slot("__ne__", "tp$richcompare", "richcmpfunc"),
  176. Slot("__gt__", "tp$richcompare", "richcmpfunc"),
  177. Slot("__ge__", "tp$richcompare", "richcmpfunc"),
  178. Slot("__richcompare__", "tp$richcompare", "richcmpfunc"),
  179. # number methods:
  180. Slot("__add__", "nb$add", "binary_nb", index=0,
  181. opcode="BINARY_ADD"),
  182. Slot("__radd__", "nb$add", "binary_nb", index=1),
  183. Slot("__sub__", "nb$subtract", "binary_nb", index=0,
  184. opcode="BINARY_SUBTRACT"),
  185. Slot("__rsub__", "nb$subtract", "binary_nb", index=1),
  186. Slot("__mul__", "nb$multiply", "binary_nb", index=0),
  187. Slot("__rmul__", "nb$multiply", "binary_nb", index=1),
  188. Slot("__div__", "nb$divide", "binary_nb", index=0,
  189. opcode="BINARY_DIVIDE"),
  190. Slot("__rdiv__", "nb$divide", "binary_nb", index=1),
  191. Slot("__mod__", "nb$remainder", "binary_nb", index=0,
  192. opcode="BINARY_MODULO"),
  193. Slot("__rmod__", "nb$remainder", "binary_nb", index=1),
  194. Slot("__divmod__", "nb$divmod", "binary_nb", index=0),
  195. Slot("__rdivmod__", "nb$divmod", "binary_nb", index=1),
  196. Slot("__lshift__", "nb$lshift", "binary_nb", index=0,
  197. opcode="BINARY_LSHIFT"),
  198. Slot("__rlshift__", "nb$lshift", "binary_nb", index=1),
  199. Slot("__rshift__", "nb$rshift", "binary_nb", index=0,
  200. opcode="BINARY_RSHIFT"),
  201. Slot("__rrshift__", "nb$rshift", "binary_nb", index=1),
  202. Slot("__and__", "nb$and", "binary_nb", index=0,
  203. opcode="BINARY_AND"),
  204. Slot("__rand__", "nb$and", "binary_nb", index=1),
  205. Slot("__xor__", "nb$xor", "binary_nb", index=0,
  206. opcode="BINARY_XOR"),
  207. Slot("__rxor__", "nb$xor", "binary_nb", index=1),
  208. Slot("__or__", "nb$or", "binary_nb", index=0,
  209. opcode="BINARY_OR"),
  210. Slot("__ror__", "nb$or", "binary_nb", index=1),
  211. # needs Py_TPFLAGS_HAVE_CLASS:
  212. Slot("__floordiv__", "nb$floor_divide", "binary_nb", index=0,
  213. opcode="BINARY_FLOOR_DIVIDE"),
  214. Slot("__rfloordiv__", "nb$floor_divide", "binary_nb", index=1),
  215. Slot("__truediv__", "nb$true_divide", "binary_nb", index=0,
  216. opcode="BINARY_TRUE_DIVIDE"),
  217. Slot("__rtruediv__", "nb$true_divide", "binary_nb", index=1),
  218. Slot("__pow__", "nb$power", "ternary",
  219. opcode="BINARY_POWER"),
  220. Slot("__rpow__", "nb$power", "ternary"), # needs wrap_tenary_nb
  221. Slot("__neg__", "nb$negative", "unary",
  222. opcode="UNARY_NEGATIVE"),
  223. Slot("__pos__", "nb$positive", "unary",
  224. opcode="UNARY_POSITIVE"),
  225. Slot("__abs__", "nb$absolute", "unary"),
  226. Slot("__nonzero__", "nb$nonzero", "inquiry"), # inverse of UNARY_NOT opcode
  227. Slot("__invert__", "nb$invert", "unary",
  228. opcode="UNARY_INVERT"),
  229. Slot("__coerce__", "nb$coerce", "coercion"), # not needed
  230. Slot("__int__", "nb$int", "unary"), # expects exact int as return
  231. Slot("__long__", "nb$long", "unary"), # expects exact long as return
  232. Slot("__float__", "nb$float", "unary"), # expects exact float as return
  233. Slot("__oct__", "nb$oct", "unary"),
  234. Slot("__hex__", "nb$hex", "unary"),
  235. # Added in 2.0. These are probably largely useless.
  236. # (For list concatenation, use sl_inplace_concat)
  237. Slot("__iadd__", "nb$inplace_add", "binary",
  238. opcode="INPLACE_ADD"),
  239. Slot("__isub__", "nb$inplace_subtract", "binary",
  240. opcode="INPLACE_SUBTRACT"),
  241. Slot("__imul__", "nb$inplace_multiply", "binary",
  242. opcode="INPLACE_MULTIPLY"),
  243. Slot("__idiv__", "nb$inplace_divide", "binary",
  244. opcode="INPLACE_DIVIDE"),
  245. Slot("__imod__", "nb$inplace_remainder", "binary",
  246. opcode="INPLACE_MODULO"),
  247. Slot("__ipow__", "nb$inplace_power", "ternary",
  248. opcode="INPLACE_POWER"),
  249. Slot("__ilshift__", "nb$inplace_lshift", "binary",
  250. opcode="INPLACE_LSHIFT"),
  251. Slot("__irshift__", "nb$inplace_rshift", "binary",
  252. opcode="INPLACE_RSHIFT"),
  253. Slot("__iand__", "nb$inplace_and", "binary",
  254. opcode="INPLACE_AND"),
  255. Slot("__ixor__", "nb$inplace_xor", "binary",
  256. opcode="INPLACE_XOR"),
  257. Slot("__ior__", "nb$inplace_or", "binary",
  258. opcode="INPLACE_OR"),
  259. Slot("__ifloordiv__", "nb$inplace_floor_divide", "binary",
  260. opcode="INPLACE_FLOOR_DIVIDE"),
  261. Slot("__itruediv__", "nb$inplace_true_divide", "binary",
  262. opcode="INPLACE_TRUE_DIVIDE"),
  263. # Added in 2.5. Used whenever i acts as a sequence index (a[i])
  264. Slot("__index__", "nb$index", "unary"), # needs int/long return
  265. # mapping
  266. # __getitem__: Python first tries mp$subscript, then sq$item
  267. # __len__: Python first tries sq$length, then mp$length
  268. # __delitem__: Reuses __setitem__ slot.
  269. Slot("__getitem__", "mp$subscript", "binary",
  270. opcode="BINARY_SUBSCR"),
  271. Slot("__delitem__", "mp$ass_subscript", "objobjargproc", index=0),
  272. Slot("__setitem__", "mp$ass_subscript", "objobjargproc", index=1),
  273. Slot("__len__", "mp$length", "len"),
  274. # sequence
  275. Slot("__contains__", "sq$contains", "objobjproc"),
  276. # These sequence methods are duplicates of number or mapping methods.
  277. # For example, in the C API, "add" can be implemented either by sq$concat,
  278. # or by np_add. Python will try both. The opcode mapping is identical
  279. # between the two. So e.g. the implementation of the BINARY_SUBSCR opcode in
  280. # Python/ceval.c will try both sq$item and mp$subscript, which is why this
  281. # opcode appears twice in our list.
  282. Slot("__add__", "sq$concat", "binary",
  283. opcode="BINARY_ADD"),
  284. Slot("__mul__", "sq$repeat", "indexargfunc",
  285. opcode="BINARY_MULTIPLY"),
  286. Slot("__iadd__", "sq$inplace_concat", "binary",
  287. opcode="INPLACE_ADD"),
  288. Slot("__imul__", "sq$inplace_repeat", "indexargfunc",
  289. opcode="INPLACE_MUL"),
  290. Slot("__getitem__", "sq$item", "sq$item",
  291. opcode="BINARY_SUBSCR"),
  292. Slot("__setitem__", "sq$ass_slice", "sq$ass_item"),
  293. Slot("__delitem__", "sq$ass_item", "sq$delitem"),
  294. # slices are passed as explicit slice objects to mp$subscript.
  295. Slot("__getslice__", "sq$slice", "sq$slice"),
  296. Slot("__setslice__", "sq$ass_slice", "ssizessizeobjarg"),
  297. Slot("__delslice__", "sq$ass_slice", "delslice"),
  298. ]
  299. This list may or may not be complete it comes from: https://github.com/google/pytypedecl/blob/master/slots.py