chalk.h 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  1. /*++
  2. Copyright (c) 2016 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. chalk.h
  9. Abstract:
  10. This header contains definitions for the Chalk scripting language.
  11. Author:
  12. Evan Green 26-May-2016
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. //
  18. // --------------------------------------------------------------------- Macros
  19. //
  20. #define CK_ASSERT(_Condition) assert(_Condition)
  21. //
  22. // This macro pops the top value off the stack and discards it.
  23. //
  24. #define CkStackPop(_Vm) CkStackRemove((_Vm), -1)
  25. //
  26. // These macros evaluate to non-zero if the value at the given stack index is
  27. // of the named type.
  28. //
  29. #define CkIsNull(_Vm, _StackIndex) \
  30. (CkGetType((_Vm), (_StackIndex)) == CkTypeNull)
  31. #define CkIsInteger(_Vm, _StackIndex) \
  32. (CkGetType((_Vm), (_StackIndex)) == CkTypeInteger)
  33. #define CkIsString(_Vm, _StackIndex) \
  34. (CkGetType((_Vm), (_StackIndex)) == CkTypeString)
  35. #define CkIsDict(_Vm, _StackIndex) \
  36. (CkGetType((_Vm), (_StackIndex)) == CkTypeDict)
  37. #define CkIsList(_Vm, _StackIndex) \
  38. (CkGetType((_Vm), (_StackIndex)) == CkTypeList)
  39. #define CkIsFunction(_Vm, _StackIndex) \
  40. (CkGetType((_Vm), (_StackIndex)) == CkTypeFunction)
  41. #define CkIsObject(_Vm, _StackIndex) \
  42. (CkGetType((_Vm), (_StackIndex)) == CkTypeObject)
  43. #define CkIsData(_Vm, _StackIndex) \
  44. (CkGetType((_Vm), (_StackIndex)) == CkTypeData)
  45. //
  46. // ---------------------------------------------------------------- Definitions
  47. //
  48. #ifndef CK_API
  49. #define CK_API __DLLIMPORT
  50. #endif
  51. #define CHALK_VERSION_MAJOR 1
  52. #define CHALK_VERSION_MINOR 0
  53. #define CHALK_VERSION_REVISION 0
  54. #define CHALK_VERSION ((CHALK_VERSION_MAJOR << 24) | \
  55. (CHALK_VERSION_MINOR << 16) | \
  56. CHALK_VERSION_REVISION)
  57. #define CK_SOURCE_EXTENSION "ck"
  58. #define CK_OBJECT_EXTENSION "cko"
  59. #define CK_MODULE_ENTRY_NAME "CkModuleInit"
  60. //
  61. // Define Chalk configuration flags.
  62. //
  63. //
  64. // Define this flag to perform a garbage collection after every allocation.
  65. //
  66. #define CK_CONFIGURATION_GC_STRESS 0x00000001
  67. //
  68. // Define this flag to print the bytecode for all compiled functions.
  69. //
  70. #define CK_CONFIGURATION_DEBUG_COMPILER 0x00000002
  71. //
  72. // Define the maximum UTF-8 value that can be encoded.
  73. //
  74. #define CK_MAX_UTF8 0x10FFFF
  75. //
  76. // ------------------------------------------------------ Data Type Definitions
  77. //
  78. typedef enum _CK_ERROR_TYPE {
  79. CkSuccess,
  80. CkErrorNoMemory,
  81. CkErrorCompile,
  82. CkErrorRuntime
  83. } CK_ERROR_TYPE, *PCK_ERROR_TYPE;
  84. typedef enum _CK_LOAD_MODULE_RESULT {
  85. CkLoadModuleSource,
  86. CkLoadModuleForeign,
  87. CkLoadModuleNotFound,
  88. CkLoadModuleNoMemory,
  89. CkLoadModuleNotSupported,
  90. CkLoadModuleStaticError,
  91. CkLoadModuleFreeError
  92. } CK_LOAD_MODULE_RESULT, *PCK_LOAD_MODULE_RESULT;
  93. //
  94. // Define the data types to the C API.
  95. //
  96. typedef enum _CK_API_TYPE {
  97. CkTypeInvalid,
  98. CkTypeNull,
  99. CkTypeInteger,
  100. CkTypeString,
  101. CkTypeDict,
  102. CkTypeList,
  103. CkTypeFunction,
  104. CkTypeObject,
  105. CkTypeData,
  106. CkTypeCount
  107. } CK_API_TYPE, *PCK_API_TYPE;
  108. typedef struct _CK_VM CK_VM, *PCK_VM;
  109. typedef LONGLONG CK_INTEGER, *PCK_INTEGER;
  110. typedef
  111. PVOID
  112. (*PCK_REALLOCATE) (
  113. PVOID Allocation,
  114. UINTN NewSize
  115. );
  116. /*++
  117. Routine Description:
  118. This routine represents the prototype of the function called when Chalk
  119. needs to allocate, reallocate, or free memory.
  120. Arguments:
  121. Allocation - Supplies an optional pointer to the allocation to resize or
  122. free. If NULL, then this routine will allocate new memory.
  123. NewSize - Supplies the size of the desired allocation. If this is 0 and the
  124. allocation parameter is non-null, the given allocation will be freed.
  125. Otherwise it will be resized to requested size.
  126. Return Value:
  127. Returns a pointer to the allocation on success.
  128. NULL on allocation failure, or in the case the memory is being freed.
  129. --*/
  130. typedef
  131. VOID
  132. (*PCK_FOREIGN_FUNCTION) (
  133. PCK_VM Vm
  134. );
  135. /*++
  136. Routine Description:
  137. This routine represents the prototype of a Chalk function implemented in C.
  138. It is the function call interface between Chalk and C.
  139. Arguments:
  140. Vm - Supplies a pointer to the virtual machine.
  141. Return Value:
  142. None. The return value of the function should be in the first stack slot.
  143. --*/
  144. typedef
  145. VOID
  146. (*PCK_DESTROY_DATA) (
  147. PVOID Data
  148. );
  149. /*++
  150. Routine Description:
  151. This routine is called to destroy a foreign data object previously created.
  152. Arguments:
  153. Data - Supplies a pointer to the class context to destroy.
  154. Return Value:
  155. None.
  156. --*/
  157. /*++
  158. Structure Description:
  159. This structure stores a loaded module in the form of source code.
  160. Members:
  161. Path - Stores a pointer to the full path of the file containing the source.
  162. The VM will free this memory when finished with this memory.
  163. PathLength - Stores the length of the full path, not including the null
  164. terminator.
  165. Text - Stores a pointer to the heap allocated source for the module. The
  166. VM will call its free function when it's through with this memory.
  167. Length - Stores the size of the source in bytes, not including a null
  168. terminator that is expected to be at the end.
  169. --*/
  170. typedef struct _CK_MODULE_SOURCE {
  171. PSTR Path;
  172. UINTN PathLength;
  173. PSTR Text;
  174. UINTN Length;
  175. } CK_MODULE_SOURCE, *PCK_MODULE_SOURCE;
  176. /*++
  177. Structure Description:
  178. This structure stores a loaded foreign module.
  179. Members:
  180. Path - Stores a pointer to the full path of the file containing the library.
  181. The VM will free this memory when finished with this memory.
  182. PathLength - Stores the length of the full path, not including the null
  183. terminator.
  184. Handle - Stores a context pointer often used to store the dynamic library
  185. handle.
  186. Entry - Stores a pointer to a function used to load the module. More
  187. precisely, it is the foreign function called when the module's fiber is
  188. run. It will be called with a single argument, the module object.
  189. --*/
  190. typedef struct _CK_FOREIGN_MODULE {
  191. PSTR Path;
  192. UINTN PathLength;
  193. PVOID Handle;
  194. PCK_FOREIGN_FUNCTION Entry;
  195. } CK_FOREIGN_MODULE, *PCK_FOREIGN_MODULE;
  196. /*++
  197. Union Description:
  198. This union stores the data resulting from an attempt to load a module.
  199. Members:
  200. Source - Stores the loaded module in source form.
  201. Foreign - Stores the loaded foreign module.
  202. Error - Stores a pointer to an error string describing why the module could
  203. not be loaded. If the error type is static, this string will not be
  204. freed. Otherwise it will be.
  205. --*/
  206. typedef union _CK_MODULE_HANDLE {
  207. CK_MODULE_SOURCE Source;
  208. CK_FOREIGN_MODULE Foreign;
  209. PSTR Error;
  210. } CK_MODULE_HANDLE, *PCK_MODULE_HANDLE;
  211. typedef
  212. CK_LOAD_MODULE_RESULT
  213. (*PCK_LOAD_MODULE) (
  214. PCK_VM Vm,
  215. PCSTR ModulePath,
  216. PCK_MODULE_HANDLE ModuleData
  217. );
  218. /*++
  219. Routine Description:
  220. This routine is called to load a new Chalk module.
  221. Arguments:
  222. Vm - Supplies a pointer to the virtual machine.
  223. ModulePath - Supplies a pointer to the module path to load. Directories
  224. will be separated with dots. If this contains a slash, then it is an
  225. absolute path that should be loaded directly.
  226. ModuleData - Supplies a pointer where the loaded module information will
  227. be returned on success.
  228. Return Value:
  229. Returns a load module error code.
  230. --*/
  231. typedef
  232. INT
  233. (*PCK_SAVE_MODULE) (
  234. PCK_VM Vm,
  235. PCSTR ModulePath,
  236. PCSTR FrozenData,
  237. UINTN FrozenDataSize
  238. );
  239. /*++
  240. Routine Description:
  241. This routine is called after a module is compiled, so that the caller can
  242. save the compilation object.
  243. Arguments:
  244. Vm - Supplies a pointer to the virtual machine.
  245. ModulePath - Supplies a pointer to the source file path that was just
  246. loaded.
  247. FrozenData - Supplies an opaque binary representation of the compiled
  248. module. The format of this data is unspecified and may change between
  249. revisions of the language.
  250. FrozenDataSize - Supplies the number of bytes in the frozen module data.
  251. Return Value:
  252. 0 on success.
  253. Returns a non-zero value on failure. Unless being explicitly asked to
  254. save a compilation, failures are not normally fatal, and so zero should
  255. almost always be returned.
  256. --*/
  257. typedef
  258. VOID
  259. (*PCK_WRITE) (
  260. PCK_VM Vm,
  261. PCSTR String
  262. );
  263. /*++
  264. Routine Description:
  265. This routine is called to print text in Chalk.
  266. Arguments:
  267. Vm - Supplies a pointer to the virtual machine.
  268. String - Supplies a pointer to the string to print. This routine should not
  269. modify or free this string.
  270. Return Value:
  271. None.
  272. --*/
  273. typedef
  274. VOID
  275. (*PCK_ERROR) (
  276. PCK_VM Vm,
  277. CK_ERROR_TYPE ErrorType,
  278. PSTR Message
  279. );
  280. /*++
  281. Routine Description:
  282. This routine when the Chalk interpreter experiences an error.
  283. Arguments:
  284. Vm - Supplies a pointer to the virtual machine.
  285. ErrorType - Supplies the type of error occurring.
  286. Message - Supplies a pointer to a string describing the error.
  287. Return Value:
  288. None.
  289. --*/
  290. /*++
  291. Structure Description:
  292. This structure encapsulates the configuration of the Chalk virtual machine.
  293. Members:
  294. Reallocate - Stores a pointer to a function used to allocate, reallocate,
  295. and free memory.
  296. LoadModule - Stores an optional pointer to a function used to load a Chalk
  297. module.
  298. SaveModule - Stores an optional pointer to a function used to save the
  299. compiled representation of a newly loaded module.
  300. UnloadForeignModule - Stores an optional pointer to a function called when
  301. a foreign module is being destroyed.
  302. Write - Stores an optional pointer to a function used to write output to
  303. the console. If this is NULL, output is simply discarded.
  304. Error - Stores a pointer to a function used to report errors. If NULL,
  305. errors are not reported.
  306. UnhandledException - Stores a pointer to a foreign function to call if an
  307. unhandled exception occurs. If null, a default function will be
  308. provided that prints the error.
  309. InitialHeapSize - Stores the number of bytes to allocate before triggering
  310. a garbage collection.
  311. MinimumHeapSize - Stores the minimum size of heap, used to keep garbage
  312. collections from occurring too frequently.
  313. HeapGrowthPercent - Stores the percentage the heap has to grow to trigger
  314. another garbage collection. Rather than expressing this as a number
  315. over 100, it's expressed as a number over 1024 to avoid the divide.
  316. So 50% would be 512 for instance.
  317. Flags - Stores a bitfield of flags governing the operation of the
  318. interpreter See CK_CONFIGURATION_* definitions.
  319. --*/
  320. typedef struct _CK_CONFIGURATION {
  321. PCK_REALLOCATE Reallocate;
  322. PCK_LOAD_MODULE LoadModule;
  323. PCK_SAVE_MODULE SaveModule;
  324. PCK_DESTROY_DATA UnloadForeignModule;
  325. PCK_WRITE Write;
  326. PCK_ERROR Error;
  327. PCK_FOREIGN_FUNCTION UnhandledException;
  328. UINTN InitialHeapSize;
  329. UINTN MinimumHeapSize;
  330. ULONG HeapGrowthPercent;
  331. ULONG Flags;
  332. } CK_CONFIGURATION, *PCK_CONFIGURATION;
  333. /*++
  334. Structure Description:
  335. This structure describes a variable or other data object in Chalk.
  336. Members:
  337. Type - Stores the type of object to register.
  338. Name - Stores the name used to access the object in Chalk.
  339. Value - Stores the value of the object
  340. Integer - Stores the integer value of the object. For many types, this
  341. member is ignored.
  342. --*/
  343. typedef struct _CK_VARIABLE_DESCRIPTION {
  344. CK_API_TYPE Type;
  345. PSTR Name;
  346. PVOID Value;
  347. CK_INTEGER Integer;
  348. } CK_VARIABLE_DESCRIPTION, *PCK_VARIABLE_DESCRIPTION;
  349. //
  350. // -------------------------------------------------------------------- Globals
  351. //
  352. //
  353. // -------------------------------------------------------- Function Prototypes
  354. //
  355. CK_API
  356. VOID
  357. CkInitializeConfiguration (
  358. PCK_CONFIGURATION Configuration
  359. );
  360. /*++
  361. Routine Description:
  362. This routine initializes a Chalk configuration with its default values.
  363. Arguments:
  364. Configuration - Supplies a pointer where the initialized configuration will
  365. be returned.
  366. Return Value:
  367. None.
  368. --*/
  369. CK_API
  370. PCK_VM
  371. CkCreateVm (
  372. PCK_CONFIGURATION Configuration
  373. );
  374. /*++
  375. Routine Description:
  376. This routine creates a new Chalk virtual machine context. Each VM context
  377. is entirely independent.
  378. Arguments:
  379. Configuration - Supplies an optional pointer to the configuration to use
  380. for this instance. If NULL, a default configuration will be provided.
  381. Return Value:
  382. Returns a pointer to the new VM on success.
  383. NULL on allocation or if an invalid configuration was supplied.
  384. --*/
  385. CK_API
  386. VOID
  387. CkDestroyVm (
  388. PCK_VM Vm
  389. );
  390. /*++
  391. Routine Description:
  392. This routine destroys a Chalk virtual machine.
  393. Arguments:
  394. Vm - Supplies a pointer to the virtual machine.
  395. Return Value:
  396. None.
  397. --*/
  398. CK_API
  399. CK_ERROR_TYPE
  400. CkInterpret (
  401. PCK_VM Vm,
  402. PCSTR Path,
  403. PCSTR Source,
  404. UINTN Length,
  405. LONG Line,
  406. BOOL Interactive
  407. );
  408. /*++
  409. Routine Description:
  410. This routine interprets the given Chalk source string within the context of
  411. the "main" module.
  412. Arguments:
  413. Vm - Supplies a pointer to the virtual machine.
  414. Path - Supplies an optional pointer to the path of the file containing the
  415. source being interpreted.
  416. Source - Supplies a pointer to the null terminated string containing the
  417. source to interpret.
  418. Length - Supplies the length of the source string, not including the null
  419. terminator.
  420. Line - Supplies the line number this code starts on. Supply 1 to start at
  421. the beginning.
  422. Interactive - Supplies a boolean indicating whether this is an interactive
  423. session or not. For interactive sessions, expression statements will be
  424. printed.
  425. Return Value:
  426. Chalk status.
  427. --*/
  428. CK_API
  429. VOID
  430. CkCollectGarbage (
  431. PCK_VM Vm
  432. );
  433. /*++
  434. Routine Description:
  435. This routine performs garbage collection on the given Chalk instance,
  436. freeing up unused dynamic memory as appropriate.
  437. Arguments:
  438. Vm - Supplies a pointer to the virtual machine.
  439. Return Value:
  440. Returns a pointer to the newly allocated or reallocated memory on success.
  441. NULL on allocation failure or for free operations.
  442. --*/
  443. CK_API
  444. PVOID
  445. CkGetContext (
  446. PCK_VM Vm
  447. );
  448. /*++
  449. Routine Description:
  450. This routine returns the context pointer stored inside the Chalk VM. This
  451. pointer is not used at all by Chalk, and can be used by the surrounding
  452. environment integrating Chalk.
  453. Arguments:
  454. Vm - Supplies a pointer to the virtual machine.
  455. Return Value:
  456. Returns the user context pointer.
  457. --*/
  458. CK_API
  459. PVOID
  460. CkSetContext (
  461. PCK_VM Vm,
  462. PVOID NewValue
  463. );
  464. /*++
  465. Routine Description:
  466. This routine sets the context pointer stored inside the Chalk VM. This
  467. pointer is not used at all by Chalk, and can be used by the surrounding
  468. environment integrating Chalk.
  469. Arguments:
  470. Vm - Supplies a pointer to the virtual machine.
  471. NewValue - Supplies the new context pointer value to set.
  472. Return Value:
  473. Returns the previous value.
  474. --*/
  475. CK_API
  476. BOOL
  477. CkPreloadForeignModule (
  478. PCK_VM Vm,
  479. PSTR ModuleName,
  480. PSTR Path,
  481. PVOID Handle,
  482. PCK_FOREIGN_FUNCTION LoadFunction
  483. );
  484. /*++
  485. Routine Description:
  486. This routine registers the availability of a foreign module that might not
  487. otherwise be reachable via the standard module load methods. This is often
  488. used for adding specialized modules in an embedded interpreter. The load
  489. function isn't called until someone actually imports the module from the
  490. interpreter. The loaded module is pushed onto the stack.
  491. Arguments:
  492. Vm - Supplies a pointer to the virtual machine.
  493. ModuleName - Supplies a pointer to the full "dotted.module.name". A copy of
  494. this memory will be made.
  495. Path - Supplies an optional pointer to the full path of the module. A copy
  496. of this memory will be made.
  497. Handle - Supplies an optional pointer to a handle (usually a dynamic
  498. library handle) that is used if the module is unloaded.
  499. LoadFunction - Supplies a pointer to a C function to call to load the
  500. module symbols. The function will be called on a new fiber, with the
  501. module itself in slot zero.
  502. Return Value:
  503. TRUE on success.
  504. FALSE on failure (usually allocation failure).
  505. --*/
  506. CK_API
  507. BOOL
  508. CkLoadModule (
  509. PCK_VM Vm,
  510. PCSTR ModuleName,
  511. PCSTR Path
  512. );
  513. /*++
  514. Routine Description:
  515. This routine loads (but does not run) the given module, and pushes it on
  516. the stack.
  517. Arguments:
  518. Vm - Supplies a pointer to the virtual machine.
  519. ModuleName - Supplies a pointer to the full "dotted.module.name". A copy of
  520. this memory will be made.
  521. Path - Supplies an optional pointer to the full path of the module. A copy
  522. of this memory will be made. If this is supplied, then this is the only
  523. path that is attempted when opening the module. If this is not supplied,
  524. then the standard load paths will be used. If a module by the given
  525. name is already loaded, this is ignored.
  526. Return Value:
  527. TRUE on success.
  528. FALSE on failure. In this case, an exception will have been thrown. The
  529. caller should not modify the stack anymore, and should return as soon as
  530. possible.
  531. --*/
  532. CK_API
  533. UINTN
  534. CkGetStackSize (
  535. PCK_VM Vm
  536. );
  537. /*++
  538. Routine Description:
  539. This routine returns the number of elements currently on the stack for the
  540. current frame.
  541. Arguments:
  542. Vm - Supplies a pointer to the virtual machine.
  543. Return Value:
  544. Returns the number of stack slots occupied by the current frame.
  545. --*/
  546. CK_API
  547. UINTN
  548. CkGetStackRemaining (
  549. PCK_VM Vm
  550. );
  551. /*++
  552. Routine Description:
  553. This routine returns the number of free slots remaining on the stack.
  554. Arguments:
  555. Vm - Supplies a pointer to the virtual machine.
  556. Return Value:
  557. Returns the number of stack slots available to the C API.
  558. --*/
  559. CK_API
  560. BOOL
  561. CkEnsureStack (
  562. PCK_VM Vm,
  563. UINTN Size
  564. );
  565. /*++
  566. Routine Description:
  567. This routine ensures that there are at least the given number of
  568. stack slots currently available for the C API.
  569. Arguments:
  570. Vm - Supplies a pointer to the virtual machine.
  571. Size - Supplies the number of additional stack slots needed by the C API.
  572. Return Value:
  573. TRUE on success.
  574. FALSE on allocation failure.
  575. --*/
  576. CK_API
  577. VOID
  578. CkPushValue (
  579. PCK_VM Vm,
  580. INTN StackIndex
  581. );
  582. /*++
  583. Routine Description:
  584. This routine pushes a value already on the stack to the top of the stack.
  585. Arguments:
  586. Vm - Supplies a pointer to the virtual machine.
  587. StackIndex - Supplies the stack index of the existing value to push.
  588. Negative values reference stack indices from the end of the stack.
  589. Return Value:
  590. None.
  591. --*/
  592. CK_API
  593. VOID
  594. CkStackRemove (
  595. PCK_VM Vm,
  596. INTN StackIndex
  597. );
  598. /*++
  599. Routine Description:
  600. This routine removes a value from the stack, and shifts all the other
  601. values down.
  602. Arguments:
  603. Vm - Supplies a pointer to the virtual machine.
  604. StackIndex - Supplies the stack index of the value to remove. Negative
  605. values reference stack indices from the end of the stack.
  606. Return Value:
  607. None.
  608. --*/
  609. CK_API
  610. VOID
  611. CkStackInsert (
  612. PCK_VM Vm,
  613. INTN StackIndex
  614. );
  615. /*++
  616. Routine Description:
  617. This routine adds the element at the top of the stack into the given
  618. stack position, and shifts all remaining elements over.
  619. Arguments:
  620. Vm - Supplies a pointer to the virtual machine.
  621. StackIndex - Supplies the stack index location to insert at. Negative
  622. values reference stack indices from the end of the stack.
  623. Return Value:
  624. None.
  625. --*/
  626. CK_API
  627. VOID
  628. CkStackReplace (
  629. PCK_VM Vm,
  630. INTN StackIndex
  631. );
  632. /*++
  633. Routine Description:
  634. This routine pops the value from the top of the stack and replaces the
  635. value at the given stack index with it.
  636. Arguments:
  637. Vm - Supplies a pointer to the virtual machine.
  638. StackIndex - Supplies the stack index to replace with the top of the stack.
  639. Negative values reference stack indices from the end of the stack. This
  640. is the stack index before the value is popped.
  641. Return Value:
  642. None.
  643. --*/
  644. CK_API
  645. CK_API_TYPE
  646. CkGetType (
  647. PCK_VM Vm,
  648. INTN StackIndex
  649. );
  650. /*++
  651. Routine Description:
  652. This routine returns the type of the value at the given stack index.
  653. Arguments:
  654. Vm - Supplies a pointer to the virtual machine.
  655. StackIndex - Supplies the stack index of the object to query. Negative
  656. values reference stack indices from the end of the stack.
  657. Return Value:
  658. Returns the stack type.
  659. --*/
  660. CK_API
  661. VOID
  662. CkPushNull (
  663. PCK_VM Vm
  664. );
  665. /*++
  666. Routine Description:
  667. This routine pushes a null value on the top of the stack.
  668. Arguments:
  669. Vm - Supplies a pointer to the virtual machine.
  670. Return Value:
  671. None.
  672. --*/
  673. CK_API
  674. VOID
  675. CkPushInteger (
  676. PCK_VM Vm,
  677. CK_INTEGER Integer
  678. );
  679. /*++
  680. Routine Description:
  681. This routine pushes an integer value on the top of the stack.
  682. Arguments:
  683. Vm - Supplies a pointer to the virtual machine.
  684. Integer - Supplies the integer to push.
  685. Return Value:
  686. None.
  687. --*/
  688. CK_API
  689. CK_INTEGER
  690. CkGetInteger (
  691. PCK_VM Vm,
  692. INTN StackIndex
  693. );
  694. /*++
  695. Routine Description:
  696. This routine returns an integer at the given stack index.
  697. Arguments:
  698. Vm - Supplies a pointer to the virtual machine.
  699. StackIndex - Supplies the stack index of the object to get. Negative
  700. values reference stack indices from the end of the stack.
  701. Return Value:
  702. Returns the integer value.
  703. 0 if the value at the stack is not an integer.
  704. --*/
  705. CK_API
  706. VOID
  707. CkPushString (
  708. PCK_VM Vm,
  709. PCSTR String,
  710. UINTN Length
  711. );
  712. /*++
  713. Routine Description:
  714. This routine pushes a string value on the top of the stack.
  715. Arguments:
  716. Vm - Supplies a pointer to the virtual machine.
  717. String - Supplies a pointer to the string data to push. A copy of this
  718. string will be made.
  719. Length - Supplies the length of the string in bytes, not including the
  720. null terminator.
  721. Return Value:
  722. None.
  723. --*/
  724. CK_API
  725. PCSTR
  726. CkGetString (
  727. PCK_VM Vm,
  728. UINTN StackIndex,
  729. PUINTN Length
  730. );
  731. /*++
  732. Routine Description:
  733. This routine returns a string at the given stack index.
  734. Arguments:
  735. Vm - Supplies a pointer to the virtual machine.
  736. StackIndex - Supplies the stack index of the object to get. Negative
  737. values reference stack indices from the end of the stack.
  738. Length - Supplies an optional pointer where the length of the string will
  739. be returned, not including a null terminator. If the value at the stack
  740. index is not a string, 0 is returned here.
  741. Return Value:
  742. Returns a pointer to the string. The caller must not modify or free this
  743. value.
  744. NULL if the value at the specified stack index is not a string.
  745. --*/
  746. CK_API
  747. VOID
  748. CkPushSubstring (
  749. PCK_VM Vm,
  750. INTN StackIndex,
  751. INTN Start,
  752. INTN End
  753. );
  754. /*++
  755. Routine Description:
  756. This routine creates a new string consisting of a portion of the string
  757. at the given stack index, and pushes it on the stack. If the value at the
  758. given stack index is not a string, then an empty string is pushed as the
  759. result. If either the start or end indices are out of range, they are
  760. adjusted to be in range.
  761. Arguments:
  762. Vm - Supplies a pointer to the virtual machine.
  763. StackIndex - Supplies the stack index of the string to slice. Negative
  764. values reference stack indices from the end of the stack.
  765. Start - Supplies the starting index of the substring, inclusive. Negative
  766. values reference from the end of the string, with -1 being after the
  767. last character of the string.
  768. End - Supplies the ending index of the substring, exclusive. Negative
  769. values reference from the end of the string, with -1 being after the
  770. last character of the string.
  771. Return Value:
  772. None.
  773. --*/
  774. CK_API
  775. VOID
  776. CkStringConcatenate (
  777. PCK_VM Vm,
  778. UINTN Count
  779. );
  780. /*++
  781. Routine Description:
  782. This routine pops a given number of strings off the stack and concatenates
  783. them. The resulting string is then pushed on the stack.
  784. Arguments:
  785. Vm - Supplies a pointer to the virtual machine.
  786. Count - Supplies the number of strings to pop off the stack.
  787. Return Value:
  788. None.
  789. --*/
  790. CK_API
  791. PVOID
  792. CkPushStringBuffer (
  793. PCK_VM Vm,
  794. UINTN MaxLength
  795. );
  796. /*++
  797. Routine Description:
  798. This routine creates an uninitialized string and pushes it on the top of
  799. the stack. The string must be finalized before use in the Chalk environment.
  800. Once finalized, the string buffer must not be modified.
  801. Arguments:
  802. Vm - Supplies a pointer to the virtual machine.
  803. MaxLength - Supplies the maximum length of the string buffer, not including
  804. a null terminator.
  805. Return Value:
  806. Returns a pointer to the string buffer on success.
  807. NULL on allocation failure.
  808. --*/
  809. CK_API
  810. VOID
  811. CkFinalizeString (
  812. PCK_VM Vm,
  813. INTN StackIndex,
  814. UINTN Length
  815. );
  816. /*++
  817. Routine Description:
  818. This routine finalizes a string that was previously created as a buffer.
  819. The string must not be modified after finalization.
  820. Arguments:
  821. Vm - Supplies a pointer to the virtual machine.
  822. StackIndex - Supplies the stack index of the string to slice. Negative
  823. values reference stack indices from the end of the stack.
  824. Length - Supplies the final length of the string, not including the null
  825. terminator. This must not be greater than the initial maximum length
  826. provided when the string buffer was pushed.
  827. Return Value:
  828. None.
  829. --*/
  830. CK_API
  831. VOID
  832. CkPushDict (
  833. PCK_VM Vm
  834. );
  835. /*++
  836. Routine Description:
  837. This routine creates a new empty dictionary and pushes it onto the stack.
  838. Arguments:
  839. Vm - Supplies a pointer to the virtual machine.
  840. Return Value:
  841. None.
  842. --*/
  843. CK_API
  844. BOOL
  845. CkDictGet (
  846. PCK_VM Vm,
  847. INTN StackIndex
  848. );
  849. /*++
  850. Routine Description:
  851. This routine pops a key off the stack, and uses it to get the corresponding
  852. value for the dictionary stored at the given stack index. The resulting
  853. value is pushed onto the stack. If no value exists for the given key, then
  854. nothing is pushed.
  855. Arguments:
  856. Vm - Supplies a pointer to the virtual machine.
  857. StackIndex - Supplies the stack index of the dictionary (before the key is
  858. popped off). Negative values reference stack indices from the end of
  859. the stack.
  860. Return Value:
  861. TRUE if there was a value for that key.
  862. FALSE if the dictionary has no contents for that value.
  863. --*/
  864. CK_API
  865. VOID
  866. CkDictSet (
  867. PCK_VM Vm,
  868. INTN StackIndex
  869. );
  870. /*++
  871. Routine Description:
  872. This routine pops a key and then a value off the stack, then sets that
  873. key-value pair in the dictionary at the given stack index.
  874. Arguments:
  875. Vm - Supplies a pointer to the virtual machine.
  876. StackIndex - Supplies the stack index of the dictionary (before anything is
  877. popped off). Negative values reference stack indices from the end of
  878. the stack.
  879. Return Value:
  880. None.
  881. --*/
  882. CK_API
  883. VOID
  884. CkDictRemove (
  885. PCK_VM Vm,
  886. INTN StackIndex
  887. );
  888. /*++
  889. Routine Description:
  890. This routine pops a key off the stack, and removes that key and
  891. corresponding value from the dictionary. No error is raised if the key
  892. did not previously exist in the dictionary.
  893. Arguments:
  894. Vm - Supplies a pointer to the virtual machine.
  895. StackIndex - Supplies the stack index of the dictionary (before anything is
  896. popped off). Negative values reference stack indices from the end of
  897. the stack.
  898. Return Value:
  899. None.
  900. --*/
  901. CK_API
  902. UINTN
  903. CkDictSize (
  904. PCK_VM Vm,
  905. INTN StackIndex
  906. );
  907. /*++
  908. Routine Description:
  909. This routine returns the size of the dictionary at the given stack index.
  910. Arguments:
  911. Vm - Supplies a pointer to the virtual machine.
  912. StackIndex - Supplies the stack index of the dictionary. Negative values
  913. reference stack indices from the end of the stack.
  914. Return Value:
  915. Returns the number of elements in the dictionary.
  916. 0 if the list is empty or the referenced item is not a dictionary.
  917. --*/
  918. CK_API
  919. BOOL
  920. CkDictIterate (
  921. PCK_VM Vm,
  922. INTN StackIndex
  923. );
  924. /*++
  925. Routine Description:
  926. This routine advances a dictionary iterator at the top of the stack. It
  927. pushes the next key and then the next value onto the stack, if there are
  928. more elements in the dictionary. Callers should push a null value onto
  929. the stack as the initial iterator before calling this routine for the first
  930. time. Callers are responsible for popping the value, key, and potentially
  931. finished iterator off the stack. Callers should not modify a dictionary
  932. during iteration, as the results are undefined.
  933. Arguments:
  934. Vm - Supplies a pointer to the virtual machine.
  935. StackIndex - Supplies the stack index of the dictionary. Negative values
  936. reference stack indices from the end of the stack.
  937. Return Value:
  938. TRUE if the next key and value were pushed on.
  939. FALSE if there are no more elements, the iterator value is invalid, or the
  940. item at the given stack index is not a dictionary.
  941. --*/
  942. CK_API
  943. VOID
  944. CkPushList (
  945. PCK_VM Vm
  946. );
  947. /*++
  948. Routine Description:
  949. This routine creates a new empty list and pushes it onto the stack.
  950. Arguments:
  951. Vm - Supplies a pointer to the virtual machine.
  952. Return Value:
  953. None.
  954. --*/
  955. CK_API
  956. VOID
  957. CkListGet (
  958. PCK_VM Vm,
  959. INTN StackIndex,
  960. INTN ListIndex
  961. );
  962. /*++
  963. Routine Description:
  964. This routine gets the value at the given list index, and pushes it on the
  965. stack.
  966. Arguments:
  967. Vm - Supplies a pointer to the virtual machine.
  968. StackIndex - Supplies the stack index of the list. Negative values
  969. reference stack indices from the end of the stack.
  970. ListIndex - Supplies the list index to get. If this index is out of bounds,
  971. the null will be pushed.
  972. Return Value:
  973. None.
  974. --*/
  975. CK_API
  976. VOID
  977. CkListSet (
  978. PCK_VM Vm,
  979. INTN StackIndex,
  980. INTN ListIndex
  981. );
  982. /*++
  983. Routine Description:
  984. This routine pops the top value off the stack, and saves it to a specific
  985. index in a list.
  986. Arguments:
  987. Vm - Supplies a pointer to the virtual machine.
  988. StackIndex - Supplies the stack index of the list. Negative values
  989. reference stack indices from the end of the stack.
  990. ListIndex - Supplies the list index to set. If this index is one beyond the
  991. end, then the value will be appended. If this index is otherwise out of
  992. bounds, the item at the top of the stack will simply be discarded.
  993. Return Value:
  994. None.
  995. --*/
  996. CK_API
  997. UINTN
  998. CkListSize (
  999. PCK_VM Vm,
  1000. INTN StackIndex
  1001. );
  1002. /*++
  1003. Routine Description:
  1004. This routine returns the size of the list at the given stack index.
  1005. Arguments:
  1006. Vm - Supplies a pointer to the virtual machine.
  1007. StackIndex - Supplies the stack index of the list. Negative values
  1008. reference stack indices from the end of the stack.
  1009. Return Value:
  1010. Returns the number of elements in the list.
  1011. 0 if the list is empty or the referenced item is not a list.
  1012. --*/
  1013. CK_API
  1014. BOOL
  1015. CkPushData (
  1016. PCK_VM Vm,
  1017. PVOID Data,
  1018. PCK_DESTROY_DATA DestroyRoutine
  1019. );
  1020. /*++
  1021. Routine Description:
  1022. This routine pushes an opaque pointer onto the stack.
  1023. Arguments:
  1024. Vm - Supplies a pointer to the virtual machine.
  1025. Data - Supplies the pointer to encapsulate.
  1026. DestroyRoutine - Supplies an optional pointer to a function to call if this
  1027. value is garbage collected.
  1028. Return Value:
  1029. TRUE on success.
  1030. FALSE on allocation failure.
  1031. --*/
  1032. CK_API
  1033. PVOID
  1034. CkGetData (
  1035. PCK_VM Vm,
  1036. INTN StackIndex
  1037. );
  1038. /*++
  1039. Routine Description:
  1040. This routine returns a data pointer that is stored the given stack index.
  1041. Arguments:
  1042. Vm - Supplies a pointer to the virtual machine.
  1043. StackIndex - Supplies the stack index of the object to get. Negative
  1044. values reference stack indices from the end of the stack.
  1045. Return Value:
  1046. Returns the opaque pointer passed in when the object was created.
  1047. NULL if the value at the stack was not a foreign data object.
  1048. --*/
  1049. CK_API
  1050. VOID
  1051. CkPushClass (
  1052. PCK_VM Vm,
  1053. INTN ModuleIndex,
  1054. ULONG FieldCount
  1055. );
  1056. /*++
  1057. Routine Description:
  1058. This routine pops a class and a string off the stack, creates a new class,
  1059. and pushes it onto the stack. The popped class is the superclass of the
  1060. new class, and the popped string is the name of the class.
  1061. Arguments:
  1062. Vm - Supplies a pointer to the virtual machine.
  1063. ModuleIndex - Supplies the stack index of the module to create the class in,
  1064. before any items are popped from the stack.
  1065. FieldCount - Supplies the number of fields to allocate for each instance of
  1066. the class. When a new class is created, these fields start out as null.
  1067. Return Value:
  1068. None.
  1069. --*/
  1070. CK_API
  1071. VOID
  1072. CkPushFunction (
  1073. PCK_VM Vm,
  1074. PCK_FOREIGN_FUNCTION Function,
  1075. PSTR Name,
  1076. ULONG ArgumentCount,
  1077. INTN ModuleIndex
  1078. );
  1079. /*++
  1080. Routine Description:
  1081. This routine pushes a C function onto the stack.
  1082. Arguments:
  1083. Vm - Supplies a pointer to the virtual machine.
  1084. Function - Supplies a pointer to the C function.
  1085. Name - Supplies a pointer to a null terminated string containing the name
  1086. of the function, used for debugging purposes. This name is not actually
  1087. assigned in the Chalk namespace.
  1088. ArgumentCount - Supplies the number of arguments the function takes, not
  1089. including the receiver slot.
  1090. ModuleIndex - Supplies the index of the module this function should be
  1091. defined within. Functions must be tied to modules to ensure that the
  1092. module containing the C function is not garbage collected and unloaded.
  1093. Return Value:
  1094. None.
  1095. --*/
  1096. CK_API
  1097. VOID
  1098. CkBindMethod (
  1099. PCK_VM Vm,
  1100. INTN ClassIndex
  1101. );
  1102. /*++
  1103. Routine Description:
  1104. This routine pops a string and then a function off the stack. It binds the
  1105. function as a class method. The class is indicated by the given stack index
  1106. (before either of the pops). The function may be either a C or Chalk
  1107. function.
  1108. Arguments:
  1109. Vm - Supplies a pointer to the virtual machine.
  1110. ClassIndex - Supplies the stack index of the class to bind the function to.
  1111. Negative values reference stack indices from the end of the stack.
  1112. Return Value:
  1113. None.
  1114. --*/
  1115. CK_API
  1116. VOID
  1117. CkGetField (
  1118. PCK_VM Vm,
  1119. UINTN FieldIndex
  1120. );
  1121. /*++
  1122. Routine Description:
  1123. This routine gets the value from the instance field with the given index,
  1124. and pushes it on the stack. This only applies to bound methods, and
  1125. operates on the receiver ("this"). If the current method is not a bound
  1126. method, or the field is out of bounds, null is pushed.
  1127. Arguments:
  1128. Vm - Supplies a pointer to the virtual machine.
  1129. FieldIndex - Supplies the field index of the instance to get.
  1130. Return Value:
  1131. None.
  1132. --*/
  1133. CK_API
  1134. VOID
  1135. CkSetField (
  1136. PCK_VM Vm,
  1137. UINTN FieldIndex
  1138. );
  1139. /*++
  1140. Routine Description:
  1141. This routine pops the top value off the stack, and saves it to a specific
  1142. field index in the function receiver. This function only applies to bound
  1143. methods. If the current function is unbound or the field index is out of
  1144. bounds, the value is popped and discarded.
  1145. Arguments:
  1146. Vm - Supplies a pointer to the virtual machine.
  1147. FieldIndex - Supplies the field index of the intance to get.
  1148. Return Value:
  1149. None.
  1150. --*/
  1151. CK_API
  1152. VOID
  1153. CkGetVariable (
  1154. PCK_VM Vm,
  1155. INTN StackIndex,
  1156. PCSTR Name
  1157. );
  1158. /*++
  1159. Routine Description:
  1160. This routine gets a global variable and pushes it on the stack. If the
  1161. variable does not exist in the given module, or the given stack index is
  1162. not a module, then null is pushed.
  1163. Arguments:
  1164. Vm - Supplies a pointer to the virtual machine.
  1165. StackIndex - Supplies the stack index of the module to look in. Negative
  1166. values reference stack indices from the end of the stack.
  1167. Name - Supplies a pointer to the null terminated string containing the
  1168. name of the variable to get.
  1169. Return Value:
  1170. None.
  1171. --*/
  1172. CK_API
  1173. VOID
  1174. CkSetVariable (
  1175. PCK_VM Vm,
  1176. INTN StackIndex,
  1177. PCSTR Name
  1178. );
  1179. /*++
  1180. Routine Description:
  1181. This routine pops the top value off the stack, and saves it to a global
  1182. variable with the given name in the given module. If the variable did not
  1183. exist previously, it is created.
  1184. Arguments:
  1185. Vm - Supplies a pointer to the virtual machine.
  1186. StackIndex - Supplies the stack index of the module to look in. Negative
  1187. values reference stack indices from the end of the stack.
  1188. Name - Supplies a pointer to the null terminated string containing the
  1189. name of the variable to set.
  1190. Return Value:
  1191. None.
  1192. --*/
  1193. CK_API
  1194. BOOL
  1195. CkCall (
  1196. PCK_VM Vm,
  1197. UINTN ArgumentCount
  1198. );
  1199. /*++
  1200. Routine Description:
  1201. This routine pops the given number of arguments off the stack, then pops
  1202. a callable object or class, and executes that call. The return value is
  1203. pushed onto the stack.
  1204. Arguments:
  1205. Vm - Supplies a pointer to the virtual machine.
  1206. ArgumentCount - Supplies the number of arguments to the call. The callable
  1207. object (either a function or a class) will also be popped after these
  1208. arguments.
  1209. Return Value:
  1210. TRUE on success.
  1211. FALSE if an error occurred.
  1212. --*/
  1213. CK_API
  1214. BOOL
  1215. CkCallMethod (
  1216. PCK_VM Vm,
  1217. PSTR MethodName,
  1218. UINTN ArgumentCount
  1219. );
  1220. /*++
  1221. Routine Description:
  1222. This routine pops the given number of arguments off the stack, then pops
  1223. an object, and executes the method with the given name on that object. The
  1224. return value is pushed onto the stack.
  1225. Arguments:
  1226. Vm - Supplies a pointer to the virtual machine.
  1227. MethodName - Supplies a pointer to the null terminated string containing
  1228. the name of the method to call.
  1229. ArgumentCount - Supplies the number of arguments to the call. The class
  1230. instance will also be popped after these arguments.
  1231. Return Value:
  1232. TRUE on success.
  1233. FALSE if an error occurred.
  1234. --*/
  1235. CK_API
  1236. VOID
  1237. CkRaiseException (
  1238. PCK_VM Vm,
  1239. INTN StackIndex
  1240. );
  1241. /*++
  1242. Routine Description:
  1243. This routine raises an exception. The caller must not make any more
  1244. modifications to the stack, and should return as soon as possible.
  1245. Arguments:
  1246. Vm - Supplies a pointer to the virtual machine.
  1247. StackIndex - Supplies the stack index of the exception to raise. Negative
  1248. values reference stack indices from the end of the stack.
  1249. Return Value:
  1250. None. The foreign function call frame is no longer on the execution stack.
  1251. --*/
  1252. CK_API
  1253. VOID
  1254. CkRaiseBasicException (
  1255. PCK_VM Vm,
  1256. PCSTR Type,
  1257. PCSTR MessageFormat,
  1258. ...
  1259. );
  1260. /*++
  1261. Routine Description:
  1262. This routine reports a runtime error in the current fiber. The caller must
  1263. not make any more modifications to the stack, and should return as soon as
  1264. possible.
  1265. Arguments:
  1266. Vm - Supplies a pointer to the virtual machine.
  1267. Type - Supplies the name of a builtin exception type. This type must
  1268. already be in scope.
  1269. MessageFormat - Supplies the printf message format string. The total size
  1270. of the resulting string is limited, so please be succinct.
  1271. ... - Supplies the remaining arguments.
  1272. Return Value:
  1273. None.
  1274. --*/
  1275. CK_API
  1276. VOID
  1277. CkPushModule (
  1278. PCK_VM Vm,
  1279. PSTR ModuleName
  1280. );
  1281. /*++
  1282. Routine Description:
  1283. This routine pushes the module with the given full.dotted.name onto the
  1284. stack.
  1285. Arguments:
  1286. Vm - Supplies a pointer to the virtual machine.
  1287. ModuleName - Supplies the name of the module to push. If no module by the
  1288. given name can be found, null is pushed.
  1289. Return Value:
  1290. None.
  1291. --*/
  1292. CK_API
  1293. VOID
  1294. CkPushCurrentModule (
  1295. PCK_VM Vm
  1296. );
  1297. /*++
  1298. Routine Description:
  1299. This routine pushes the module that the running function was defined in
  1300. onto the stack. If no function is currently running, then null is pushed.
  1301. Arguments:
  1302. Vm - Supplies a pointer to the virtual machine.
  1303. Return Value:
  1304. None.
  1305. --*/
  1306. CK_API
  1307. VOID
  1308. CkPushModulePath (
  1309. PCK_VM Vm
  1310. );
  1311. /*++
  1312. Routine Description:
  1313. This routine pushes the module path onto the stack.
  1314. Arguments:
  1315. Vm - Supplies a pointer to the virtual machine.
  1316. Return Value:
  1317. None.
  1318. --*/
  1319. //
  1320. // Higher level support functions
  1321. //
  1322. CK_API
  1323. BOOL
  1324. CkCheckArguments (
  1325. PCK_VM Vm,
  1326. UINTN Count,
  1327. ...
  1328. );
  1329. /*++
  1330. Routine Description:
  1331. This routine validates that the given arguments are of the correct type. If
  1332. any of them are not, it throws a nicely formatted error.
  1333. Arguments:
  1334. Vm - Supplies a pointer to the virtual machine.
  1335. Count - Supplies the number of arguments coming next.
  1336. ... - Supplies the remaining type arguments.
  1337. Return Value:
  1338. TRUE if the given arguments match the required type.
  1339. FALSE if an argument is not of the right type. In that case, an error
  1340. will be created.
  1341. --*/
  1342. CK_API
  1343. BOOL
  1344. CkCheckArgument (
  1345. PCK_VM Vm,
  1346. INTN StackIndex,
  1347. CK_API_TYPE Type
  1348. );
  1349. /*++
  1350. Routine Description:
  1351. This routine validates that the given argument is of the correct type. If
  1352. it is not, it throws a nicely formatted error.
  1353. Arguments:
  1354. Vm - Supplies a pointer to the virtual machine.
  1355. StackIndex - Supplies the stack index to check. Remember that 1 is the
  1356. first argument index.
  1357. Type - Supplies the type to check.
  1358. Return Value:
  1359. TRUE if the given argument matches the required type.
  1360. FALSE if the argument is not of the right type. In that case, an error
  1361. will be created.
  1362. --*/
  1363. CK_API
  1364. VOID
  1365. CkDeclareVariables (
  1366. PCK_VM Vm,
  1367. INTN ModuleIndex,
  1368. PCK_VARIABLE_DESCRIPTION Variables
  1369. );
  1370. /*++
  1371. Routine Description:
  1372. This routine registers an array of Chalk objects in the given module.
  1373. Arguments:
  1374. Vm - Supplies a pointer to the virtual machine.
  1375. ModuleIndex - Supplies the stack index of the module to add the variables
  1376. to.
  1377. Variables - Supplies a pointer to an array of variables. The array should
  1378. be NULL terminated.
  1379. Return Value:
  1380. None.
  1381. --*/
  1382. CK_API
  1383. VOID
  1384. CkReturnNull (
  1385. PCK_VM Vm
  1386. );
  1387. /*++
  1388. Routine Description:
  1389. This routine sets null as the return value.
  1390. Arguments:
  1391. Vm - Supplies a pointer to the virtual machine.
  1392. Return Value:
  1393. None.
  1394. --*/
  1395. CK_API
  1396. VOID
  1397. CkReturnInteger (
  1398. PCK_VM Vm,
  1399. CK_INTEGER Integer
  1400. );
  1401. /*++
  1402. Routine Description:
  1403. This routine sets an integer as the return value.
  1404. Arguments:
  1405. Vm - Supplies a pointer to the virtual machine.
  1406. Integer - Supplies the integer to set as the foreign function return.
  1407. Return Value:
  1408. None.
  1409. --*/
  1410. CK_API
  1411. VOID
  1412. CkReturnString (
  1413. PCK_VM Vm,
  1414. PCSTR String,
  1415. UINTN Length
  1416. );
  1417. /*++
  1418. Routine Description:
  1419. This routine creates a new string and sets it as the return value.
  1420. Arguments:
  1421. Vm - Supplies a pointer to the virtual machine.
  1422. String - Supplies a pointer to the buffer containing the string. A copy of
  1423. this buffer will be made.
  1424. Length - Supplies the length of the buffer, in bytes, not including the
  1425. null terminator.
  1426. Return Value:
  1427. None.
  1428. --*/
  1429. CK_API
  1430. BOOL
  1431. CkGetLength (
  1432. PCK_VM Vm,
  1433. INTN StackIndex,
  1434. PCK_INTEGER Length
  1435. );
  1436. /*++
  1437. Routine Description:
  1438. This routine returns the number of elements in the given list or dict by
  1439. calling its length method.
  1440. Arguments:
  1441. Vm - Supplies a pointer to the virtual machine.
  1442. StackIndex - Supplies the stack index of the list to get the length of.
  1443. Length - Supplies a pointer where the result of the length method will be
  1444. returned.
  1445. Return Value:
  1446. None.
  1447. --*/