core.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  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. core.c
  9. Abstract:
  10. This module implements the core of the Chalk runtime, including the base
  11. classes.
  12. Author:
  13. Evan Green 28-May-2016
  14. Environment:
  15. Any
  16. --*/
  17. //
  18. // ------------------------------------------------------------------- Includes
  19. //
  20. #include <stdio.h>
  21. #include "chalkp.h"
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. //
  26. // ------------------------------------------------------ Data Type Definitions
  27. //
  28. //
  29. // ----------------------------------------------- Internal Function Prototypes
  30. //
  31. PCK_CLASS
  32. CkpDefineCoreClass (
  33. PCK_VM Vm,
  34. PCK_MODULE Module,
  35. PSTR Name
  36. );
  37. VOID
  38. CkpCoreAddPrimitives (
  39. PCK_VM Vm,
  40. PCK_CLASS Class,
  41. PCK_PRIMITIVE_DESCRIPTION Primitives
  42. );
  43. VOID
  44. CkpCoreAddPrimitive (
  45. PCK_VM Vm,
  46. PCK_CLASS Class,
  47. PSTR Name,
  48. CK_ARITY Arity,
  49. PCK_PRIMITIVE_FUNCTION Function
  50. );
  51. BOOL
  52. CkpObjectLogicalNot (
  53. PCK_VM Vm,
  54. PCK_VALUE Arguments
  55. );
  56. BOOL
  57. CkpObjectInit (
  58. PCK_VM Vm,
  59. PCK_VALUE Arguments
  60. );
  61. BOOL
  62. CkpObjectIsEqual (
  63. PCK_VM Vm,
  64. PCK_VALUE Arguments
  65. );
  66. BOOL
  67. CkpObjectIsNotEqual (
  68. PCK_VM Vm,
  69. PCK_VALUE Arguments
  70. );
  71. BOOL
  72. CkpObjectIs (
  73. PCK_VM Vm,
  74. PCK_VALUE Arguments
  75. );
  76. BOOL
  77. CkpObjectToString (
  78. PCK_VM Vm,
  79. PCK_VALUE Arguments
  80. );
  81. BOOL
  82. CkpObjectGet (
  83. PCK_VM Vm,
  84. PCK_VALUE Arguments
  85. );
  86. BOOL
  87. CkpObjectSet (
  88. PCK_VM Vm,
  89. PCK_VALUE Arguments
  90. );
  91. BOOL
  92. CkpObjectImplements (
  93. PCK_VM Vm,
  94. PCK_VALUE Arguments
  95. );
  96. BOOL
  97. CkpObjectType (
  98. PCK_VM Vm,
  99. PCK_VALUE Arguments
  100. );
  101. BOOL
  102. CkpObjectMetaSame (
  103. PCK_VM Vm,
  104. PCK_VALUE Arguments
  105. );
  106. BOOL
  107. CkpClassName (
  108. PCK_VM Vm,
  109. PCK_VALUE Arguments
  110. );
  111. BOOL
  112. CkpClassSuper (
  113. PCK_VM Vm,
  114. PCK_VALUE Arguments
  115. );
  116. BOOL
  117. CkpNullLogicalNot (
  118. PCK_VM Vm,
  119. PCK_VALUE Arguments
  120. );
  121. BOOL
  122. CkpNullToString (
  123. PCK_VM Vm,
  124. PCK_VALUE Arguments
  125. );
  126. BOOL
  127. CkpFunctionArity (
  128. PCK_VM Vm,
  129. PCK_VALUE Arguments
  130. );
  131. BOOL
  132. CkpFunctionModule (
  133. PCK_VM Vm,
  134. PCK_VALUE Arguments
  135. );
  136. BOOL
  137. CkpFunctionStackUsage (
  138. PCK_VM Vm,
  139. PCK_VALUE Arguments
  140. );
  141. BOOL
  142. CkpCoreGarbageCollect (
  143. PCK_VM Vm,
  144. PCK_VALUE Arguments
  145. );
  146. BOOL
  147. CkpCoreImportModule (
  148. PCK_VM Vm,
  149. PCK_VALUE Arguments
  150. );
  151. BOOL
  152. CkpCoreWrite (
  153. PCK_VM Vm,
  154. PCK_VALUE Arguments
  155. );
  156. BOOL
  157. CkpCoreGetModules (
  158. PCK_VM Vm,
  159. PCK_VALUE Arguments
  160. );
  161. BOOL
  162. CkpCoreGetModulePath (
  163. PCK_VM Vm,
  164. PCK_VALUE Arguments
  165. );
  166. BOOL
  167. CkpCoreSetModulePath (
  168. PCK_VM Vm,
  169. PCK_VALUE Arguments
  170. );
  171. BOOL
  172. CkpCoreRaise (
  173. PCK_VM Vm,
  174. PCK_VALUE Arguments
  175. );
  176. BOOL
  177. CkpCoreImportAllSymbols (
  178. PCK_VM Vm,
  179. PCK_VALUE Arguments
  180. );
  181. //
  182. // -------------------------------------------------------------------- Globals
  183. //
  184. extern PVOID _binary_ckcore_ck_start;
  185. extern PVOID _binary_ckcore_ck_end;
  186. CK_PRIMITIVE_DESCRIPTION CkObjectPrimitives[] = {
  187. {"__init@0", 0, CkpObjectInit},
  188. {"__lnot@0", 0, CkpObjectLogicalNot},
  189. {"__eq@1", 1, CkpObjectIsEqual},
  190. {"__ne@1", 1, CkpObjectIsNotEqual},
  191. {"__is@1", 1, CkpObjectIs},
  192. {"__str@0", 0, CkpObjectToString},
  193. {"__repr@0", 0, CkpObjectToString},
  194. {"__get@1", 1, CkpObjectGet},
  195. {"__set@2", 2, CkpObjectSet},
  196. {"implements@2", 2, CkpObjectImplements},
  197. {"type@0", 0, CkpObjectType},
  198. {NULL, 0, NULL}
  199. };
  200. CK_PRIMITIVE_DESCRIPTION CkObjectMetaPrimitives[] = {
  201. {"same@2", 2, CkpObjectMetaSame},
  202. {NULL, 0, NULL}
  203. };
  204. CK_PRIMITIVE_DESCRIPTION CkClassPrimitives[] = {
  205. {"name@0", 0, CkpClassName},
  206. {"superType@0", 0, CkpClassSuper},
  207. {"__repr@0", 0, CkpClassName},
  208. {NULL, 0, NULL}
  209. };
  210. CK_PRIMITIVE_DESCRIPTION CkNullPrimitives[] = {
  211. {"__lnot@0", 0, CkpNullLogicalNot},
  212. {"__str@0", 0, CkpNullToString},
  213. {"__repr@0", 0, CkpNullToString},
  214. {NULL, 0, NULL}
  215. };
  216. CK_PRIMITIVE_DESCRIPTION CkFunctionPrimitives[] = {
  217. {"arity@0", 0, CkpFunctionArity},
  218. {"module@0", 0, CkpFunctionModule},
  219. {"stackUsage@0", 0, CkpFunctionStackUsage},
  220. {NULL, 0, NULL}
  221. };
  222. CK_PRIMITIVE_DESCRIPTION CkCorePrimitives[] = {
  223. {"gc@0", 0, CkpCoreGarbageCollect},
  224. {"importModule@1", 1, CkpCoreImportModule},
  225. {"_write@1", 1, CkpCoreWrite},
  226. {"modules@0", 0, CkpCoreGetModules},
  227. {"modulePath@0", 0, CkpCoreGetModulePath},
  228. {"setModulePath@1", 1, CkpCoreSetModulePath},
  229. {"raise@1", 1, CkpCoreRaise},
  230. {"importAllSymbols@1", 1, CkpCoreImportAllSymbols},
  231. {NULL, 0, NULL}
  232. };
  233. //
  234. // ------------------------------------------------------------------ Functions
  235. //
  236. CK_ERROR_TYPE
  237. CkpInitializeCore (
  238. PCK_VM Vm
  239. )
  240. /*++
  241. Routine Description:
  242. This routine initialize the Chalk VM, creating and wiring up the root
  243. classes.
  244. Arguments:
  245. Vm - Supplies a pointer to the virtual machine.
  246. Return Value:
  247. Chalk status.
  248. --*/
  249. {
  250. PCK_BUILTIN_CLASSES Classes;
  251. PCK_MODULE CoreModule;
  252. CK_ERROR_TYPE Error;
  253. PCK_OBJECT Object;
  254. PCK_CLASS ObjectMeta;
  255. UINTN Size;
  256. CK_VALUE Value;
  257. Value = CkpStringCreate(Vm, "<core>", 6);
  258. if (CK_IS_NULL(Value)) {
  259. return CkErrorNoMemory;
  260. }
  261. CoreModule = CkpModuleAllocate(Vm, CK_AS_STRING(Value), NULL);
  262. if (CoreModule == NULL) {
  263. return CkErrorNoMemory;
  264. }
  265. CK_OBJECT_VALUE(Value, CoreModule);
  266. CkpDictSet(Vm, Vm->Modules, CK_NULL_VALUE, Value);
  267. //
  268. // Create the root Object class, which inherits from itself, and whose type
  269. // is the Object metaclass.
  270. //
  271. Classes = &(Vm->Class);
  272. Classes->Object = CkpDefineCoreClass(Vm, CoreModule, "Object");
  273. if (Classes->Object == NULL) {
  274. return CkErrorNoMemory;
  275. }
  276. Classes->Object->Super = Classes->Object;
  277. CkpCoreAddPrimitives(Vm, Classes->Object, CkObjectPrimitives);
  278. //
  279. // Create the root Class class, which inherits from Object (like everything
  280. // does), and whose class is itself.
  281. //
  282. Classes->Class = CkpDefineCoreClass(Vm, CoreModule, "Class");
  283. if (Classes->Class == NULL) {
  284. return CkErrorNoMemory;
  285. }
  286. CkpBindSuperclass(Vm, Classes->Class, Classes->Object);
  287. CkpCoreAddPrimitives(Vm, Classes->Class, CkClassPrimitives);
  288. //
  289. // Create the Object metaclass, which inherits from Class.
  290. //
  291. ObjectMeta = CkpDefineCoreClass(Vm, CoreModule, "ObjectMeta");
  292. if (ObjectMeta == NULL) {
  293. return CkErrorNoMemory;
  294. }
  295. Classes->Object->Header.Class = ObjectMeta;
  296. ObjectMeta->Header.Class = Classes->Class;
  297. Classes->Class->Header.Class = Classes->Class;
  298. CkpBindSuperclass(Vm, ObjectMeta, Classes->Class);
  299. //
  300. // Define the rest of the classes using normal source.
  301. //
  302. Size = (UINTN)&_binary_ckcore_ck_end - (UINTN)&_binary_ckcore_ck_start;
  303. Error = CkpInterpret(Vm,
  304. NULL,
  305. NULL,
  306. (PSTR)&_binary_ckcore_ck_start,
  307. Size,
  308. 1,
  309. 0);
  310. if (Error != CkSuccess) {
  311. return Error;
  312. }
  313. //
  314. // Wire up the primitives to the core classes.
  315. //
  316. Value = *(CkpFindModuleVariable(Vm, CoreModule, "Fiber", FALSE));
  317. Classes->Fiber = CK_AS_CLASS(Value);
  318. CkpCoreAddPrimitives(Vm, Classes->Fiber, CkFiberPrimitives);
  319. CkpCoreAddPrimitives(Vm,
  320. Classes->Fiber->Header.Class,
  321. CkFiberStaticPrimitives);
  322. Value = *(CkpFindModuleVariable(Vm, CoreModule, "Null", FALSE));
  323. Classes->Null = CK_AS_CLASS(Value);
  324. CkpCoreAddPrimitives(Vm, Classes->Null, CkNullPrimitives);
  325. Value = *(CkpFindModuleVariable(Vm, CoreModule, "Int", FALSE));
  326. Classes->Int = CK_AS_CLASS(Value);
  327. CkpCoreAddPrimitives(Vm, Classes->Int, CkIntPrimitives);
  328. CkpCoreAddPrimitives(Vm, Classes->Int->Header.Class, CkIntStaticPrimitives);
  329. Value = *(CkpFindModuleVariable(Vm, CoreModule, "String", FALSE));
  330. Classes->String = CK_AS_CLASS(Value);
  331. CkpCoreAddPrimitives(Vm, Classes->String, CkStringPrimitives);
  332. CkpCoreAddPrimitives(Vm,
  333. Classes->String->Header.Class,
  334. CkStringStaticPrimitives);
  335. Value = *(CkpFindModuleVariable(Vm, CoreModule, "Function", FALSE));
  336. Classes->Function = CK_AS_CLASS(Value);
  337. CkpCoreAddPrimitives(Vm, Classes->Function, CkFunctionPrimitives);
  338. Value = *(CkpFindModuleVariable(Vm, CoreModule, "List", FALSE));
  339. Classes->List = CK_AS_CLASS(Value);
  340. CkpCoreAddPrimitives(Vm, Classes->List, CkListPrimitives);
  341. Value = *(CkpFindModuleVariable(Vm, CoreModule, "Dict", FALSE));
  342. Classes->Dict = CK_AS_CLASS(Value);
  343. CkpCoreAddPrimitives(Vm, Classes->Dict, CkDictPrimitives);
  344. Value = *(CkpFindModuleVariable(Vm, CoreModule, "Range", FALSE));
  345. Classes->Range = CK_AS_CLASS(Value);
  346. CkpCoreAddPrimitives(Vm, Classes->Range, CkRangePrimitives);
  347. Value = *(CkpFindModuleVariable(Vm, CoreModule, "Core", FALSE));
  348. Classes->Core = CK_AS_CLASS(Value);
  349. CkpCoreAddPrimitives(Vm, Classes->Core->Header.Class, CkCorePrimitives);
  350. Value = *(CkpFindModuleVariable(Vm, CoreModule, "Module", FALSE));
  351. Classes->Module = CK_AS_CLASS(Value);
  352. CkpCoreAddPrimitives(Vm, Classes->Module, CkModulePrimitives);
  353. Value = *(CkpFindModuleVariable(Vm, CoreModule, "Exception", FALSE));
  354. Classes->Exception = CK_AS_CLASS(Value);
  355. //
  356. // Patch up any of the core objects that may have been created before their
  357. // associated classes existed.
  358. //
  359. Object = Vm->FirstObject;
  360. while (Object != NULL) {
  361. if (Object->Type == CkObjectString) {
  362. Object->Class = Classes->String;
  363. } else if (Object->Type == CkObjectClosure) {
  364. Object->Class = Classes->Function;
  365. } else if (Object->Type == CkObjectDict) {
  366. Object->Class = Classes->Dict;
  367. } else if (Object->Type == CkObjectFiber) {
  368. Object->Class = Classes->Fiber;
  369. }
  370. Object = Object->Next;
  371. }
  372. CoreModule->Header.Class = Classes->Module;
  373. //
  374. // Set some flags on the special builtin classes.
  375. //
  376. Classes->Class->Flags |= CK_CLASS_SPECIAL_CREATION;
  377. Classes->Fiber->Flags |= CK_CLASS_UNINHERITABLE | CK_CLASS_SPECIAL_CREATION;
  378. Classes->Function->Flags |= CK_CLASS_UNINHERITABLE |
  379. CK_CLASS_SPECIAL_CREATION;
  380. Classes->List->Flags |= CK_CLASS_UNINHERITABLE | CK_CLASS_SPECIAL_CREATION;
  381. Classes->Dict->Flags |= CK_CLASS_UNINHERITABLE | CK_CLASS_SPECIAL_CREATION;
  382. Classes->Null->Flags |= CK_CLASS_UNINHERITABLE | CK_CLASS_SPECIAL_CREATION;
  383. Classes->Int->Flags |= CK_CLASS_UNINHERITABLE | CK_CLASS_SPECIAL_CREATION;
  384. Classes->Range->Flags |= CK_CLASS_UNINHERITABLE | CK_CLASS_SPECIAL_CREATION;
  385. Classes->String->Flags |=
  386. CK_CLASS_UNINHERITABLE | CK_CLASS_SPECIAL_CREATION;
  387. Classes->Module->Flags |=
  388. CK_CLASS_UNINHERITABLE | CK_CLASS_SPECIAL_CREATION;
  389. Classes->Core->Flags |= CK_CLASS_UNINHERITABLE | CK_CLASS_SPECIAL_CREATION;
  390. return CkSuccess;
  391. }
  392. CK_ARITY
  393. CkpGetFunctionArity (
  394. PCK_CLOSURE Closure
  395. )
  396. /*++
  397. Routine Description:
  398. This routine returns the number of argument required to pass to the given
  399. function.
  400. Arguments:
  401. Closure - Supplies a pointer to the closure.
  402. Return Value:
  403. Returns the arity of the function.
  404. --*/
  405. {
  406. switch (Closure->Type) {
  407. case CkClosureBlock:
  408. return Closure->U.Block.Function->Arity;
  409. case CkClosurePrimitive:
  410. return Closure->U.Primitive.Arity;
  411. case CkClosureForeign:
  412. return Closure->U.Foreign.Arity;
  413. default:
  414. CK_ASSERT(FALSE);
  415. break;
  416. }
  417. return 0;
  418. }
  419. PCK_STRING
  420. CkpGetFunctionName (
  421. PCK_CLOSURE Closure
  422. )
  423. /*++
  424. Routine Description:
  425. This routine returns the original name for a function.
  426. Arguments:
  427. Closure - Supplies a pointer to the closure.
  428. Return Value:
  429. Returns a pointer to a string containing the name of the function.
  430. --*/
  431. {
  432. switch (Closure->Type) {
  433. case CkClosureBlock:
  434. return Closure->U.Block.Function->Debug.Name;
  435. case CkClosurePrimitive:
  436. return Closure->U.Primitive.Name;
  437. case CkClosureForeign:
  438. return Closure->U.Foreign.Name;
  439. default:
  440. CK_ASSERT(FALSE);
  441. break;
  442. }
  443. return 0;
  444. }
  445. BOOL
  446. CkpObjectIsClass (
  447. PCK_CLASS ObjectClass,
  448. PCK_CLASS QueryClass
  449. )
  450. /*++
  451. Routine Description:
  452. This routine determines if the given object is of the given type.
  453. Arguments:
  454. ObjectClass - Supplies a pointer to the object class to query.
  455. QueryClass - Supplies a pointer to the class to check membership for.
  456. Return Value:
  457. TRUE if the object class is a subclass of the query class.
  458. FALSE if the object class is not a member of the query class.
  459. --*/
  460. {
  461. //
  462. // Walk up the class hierarchy comparing to the class in question.
  463. //
  464. while (TRUE) {
  465. if (ObjectClass == QueryClass) {
  466. return TRUE;
  467. } else if ((ObjectClass == NULL) ||
  468. (ObjectClass->Super == ObjectClass)) {
  469. break;
  470. }
  471. ObjectClass = ObjectClass->Super;
  472. }
  473. return FALSE;
  474. }
  475. //
  476. // --------------------------------------------------------- Internal Functions
  477. //
  478. PCK_CLASS
  479. CkpDefineCoreClass (
  480. PCK_VM Vm,
  481. PCK_MODULE Module,
  482. PSTR Name
  483. )
  484. /*++
  485. Routine Description:
  486. This routine creates a new class object for one of the base core classes.
  487. Arguments:
  488. Vm - Supplies a pointer to the virtual machine.
  489. Module - Supplies a pointer to the module the class is defined in.
  490. Name - Supplies a pointer to the null terminated name of the class.
  491. Return Value:
  492. Returns a pointer to the class on success.
  493. NULL on failure.
  494. --*/
  495. {
  496. PCK_CLASS Class;
  497. PCK_STRING NameString;
  498. CK_VALUE Value;
  499. Value = CkpStringFormat(Vm, "$", Name);
  500. if (!CK_IS_OBJECT(Value)) {
  501. return NULL;
  502. }
  503. NameString = CK_AS_STRING(Value);
  504. CkpPushRoot(Vm, &(NameString->Header));
  505. //
  506. // Allocate one field for the built-in dictionary that all objects have.
  507. //
  508. Class = CkpClassAllocate(Vm, Module, 1, NameString);
  509. if (Class == NULL) {
  510. CkpPopRoot(Vm);
  511. return NULL;
  512. }
  513. CK_OBJECT_VALUE(Value, Class);
  514. CkpDefineModuleVariable(Vm,
  515. Module,
  516. NameString->Value,
  517. NameString->Length,
  518. Value);
  519. CkpPopRoot(Vm);
  520. return Class;
  521. }
  522. VOID
  523. CkpCoreAddPrimitives (
  524. PCK_VM Vm,
  525. PCK_CLASS Class,
  526. PCK_PRIMITIVE_DESCRIPTION Primitives
  527. )
  528. /*++
  529. Routine Description:
  530. This routine adds multiple primitive functions to one of the builtin
  531. classes.
  532. Arguments:
  533. Vm - Supplies a pointer to the virtual machine.
  534. Class - Supplies a pointer to the class to add a method to.
  535. Primitives - Supplies a pointer to a null-terminated array of primitive
  536. descriptions.
  537. Return Value:
  538. None.
  539. --*/
  540. {
  541. while (Primitives->Name != NULL) {
  542. CkpCoreAddPrimitive(Vm,
  543. Class,
  544. Primitives->Name,
  545. Primitives->Arity,
  546. Primitives->Primitive);
  547. Primitives += 1;
  548. }
  549. return;
  550. }
  551. VOID
  552. CkpCoreAddPrimitive (
  553. PCK_VM Vm,
  554. PCK_CLASS Class,
  555. PSTR Name,
  556. CK_ARITY Arity,
  557. PCK_PRIMITIVE_FUNCTION Function
  558. )
  559. /*++
  560. Routine Description:
  561. This routine adds a primitive function to one of the builtin classes.
  562. Arguments:
  563. Vm - Supplies a pointer to the virtual machine.
  564. Module - Supplies a pointer to the module the class lives in.
  565. Class - Supplies a pointer to the class to add a method to.
  566. Name - Supplies a pointer to the null terminated name of the method.
  567. Arity - Supplies the number of arguments the function takes.
  568. Function - Supplies a pointer to the C function to attach to this method.
  569. Return Value:
  570. None.
  571. --*/
  572. {
  573. PCK_CLOSURE Closure;
  574. CK_SYMBOL_INDEX Index;
  575. PCK_MODULE Module;
  576. CK_VALUE NameString;
  577. Module = Class->Module;
  578. Index = CkpStringTableEnsure(Vm, &(Module->Strings), Name, strlen(Name));
  579. if (Index == -1) {
  580. return;
  581. }
  582. NameString = Module->Strings.List.Data[Index];
  583. Closure = CkpClosureCreatePrimitive(Vm,
  584. Function,
  585. Class,
  586. CK_AS_STRING(NameString),
  587. Arity);
  588. CkpBindMethod(Vm, Class, NameString, Closure);
  589. return;
  590. }
  591. BOOL
  592. CkpObjectInit (
  593. PCK_VM Vm,
  594. PCK_VALUE Arguments
  595. )
  596. /*++
  597. Routine Description:
  598. This routine contains a dummy init function that allows any object to
  599. be initialized with zero arguments. In this case all fields are null.
  600. Arguments:
  601. Vm - Supplies a pointer to the virtual machine.
  602. Arguments - Supplies the function arguments.
  603. Return Value:
  604. TRUE on success.
  605. FALSE if execution caused a runtime error.
  606. --*/
  607. {
  608. return TRUE;
  609. }
  610. BOOL
  611. CkpObjectLogicalNot (
  612. PCK_VM Vm,
  613. PCK_VALUE Arguments
  614. )
  615. /*++
  616. Routine Description:
  617. This routine determines the logical not of an object.
  618. Arguments:
  619. Vm - Supplies a pointer to the virtual machine.
  620. Arguments - Supplies the function arguments.
  621. Return Value:
  622. TRUE on success.
  623. FALSE if execution caused a runtime error.
  624. --*/
  625. {
  626. if (CkpGetValueBoolean(Arguments[0])) {
  627. Arguments[0] = CkZeroValue;
  628. } else {
  629. Arguments[0] = CkOneValue;
  630. }
  631. return TRUE;
  632. }
  633. BOOL
  634. CkpObjectIsEqual (
  635. PCK_VM Vm,
  636. PCK_VALUE Arguments
  637. )
  638. /*++
  639. Routine Description:
  640. This routine determines if two objects are equal.
  641. Arguments:
  642. Vm - Supplies a pointer to the virtual machine.
  643. Arguments - Supplies the function arguments.
  644. Return Value:
  645. TRUE on success.
  646. FALSE if execution caused a runtime error.
  647. --*/
  648. {
  649. if (CkpAreValuesEqual(Arguments[0], Arguments[1])) {
  650. Arguments[0] = CkOneValue;
  651. } else {
  652. Arguments[0] = CkZeroValue;
  653. }
  654. return TRUE;
  655. }
  656. BOOL
  657. CkpObjectIsNotEqual (
  658. PCK_VM Vm,
  659. PCK_VALUE Arguments
  660. )
  661. /*++
  662. Routine Description:
  663. This routine determines if two objects are not equal.
  664. Arguments:
  665. Vm - Supplies a pointer to the virtual machine.
  666. Arguments - Supplies the function arguments.
  667. Return Value:
  668. TRUE on success.
  669. FALSE if execution caused a runtime error.
  670. --*/
  671. {
  672. if (CkpAreValuesEqual(Arguments[0], Arguments[1])) {
  673. Arguments[0] = CkZeroValue;
  674. } else {
  675. Arguments[0] = CkOneValue;
  676. }
  677. return TRUE;
  678. }
  679. BOOL
  680. CkpObjectIs (
  681. PCK_VM Vm,
  682. PCK_VALUE Arguments
  683. )
  684. /*++
  685. Routine Description:
  686. This routine determines if the given object is of the given type.
  687. Arguments:
  688. Vm - Supplies a pointer to the virtual machine.
  689. Arguments - Supplies the function arguments.
  690. Return Value:
  691. TRUE on success.
  692. FALSE if execution caused a runtime error.
  693. --*/
  694. {
  695. UINTN Index;
  696. BOOL Is;
  697. PCK_LIST List;
  698. PCK_CLASS ObjectClass;
  699. PCK_CLASS QueryClass;
  700. CK_VALUE Value;
  701. ObjectClass = CkpGetClass(Vm, Arguments[0]);
  702. //
  703. // If a list was passed in, determine if the object is an instance of any
  704. // of the classes in the list.
  705. //
  706. if (CK_IS_LIST(Arguments[1])) {
  707. List = CK_AS_LIST(Arguments[1]);
  708. Is = FALSE;
  709. for (Index = 0; Index < List->Elements.Count; Index += 1) {
  710. Value = List->Elements.Data[Index];
  711. if (!CK_IS_CLASS(Value)) {
  712. CkpRuntimeError(Vm,
  713. "TypeError",
  714. "Expected a class");
  715. return FALSE;
  716. }
  717. QueryClass = CK_AS_CLASS(Value);
  718. Is = CkpObjectIsClass(ObjectClass, QueryClass);
  719. if (Is != FALSE) {
  720. break;
  721. }
  722. }
  723. //
  724. // If a class was passed in, see if the object is an instance of the class.
  725. //
  726. } else if (CK_IS_CLASS(Arguments[1])) {
  727. QueryClass = CK_AS_CLASS(Arguments[1]);
  728. Is = CkpObjectIsClass(ObjectClass, QueryClass);
  729. } else {
  730. CkpRuntimeError(Vm, "TypeError", "Expected a class");
  731. return FALSE;
  732. }
  733. if (Is != FALSE) {
  734. Arguments[0] = CkOneValue;
  735. } else {
  736. Arguments[0] = CkZeroValue;
  737. }
  738. return TRUE;
  739. }
  740. BOOL
  741. CkpObjectToString (
  742. PCK_VM Vm,
  743. PCK_VALUE Arguments
  744. )
  745. /*++
  746. Routine Description:
  747. This routine creates a default string representation of the given object.
  748. Arguments:
  749. Vm - Supplies a pointer to the virtual machine.
  750. Arguments - Supplies the function arguments.
  751. Return Value:
  752. TRUE on success.
  753. FALSE if execution caused a runtime error.
  754. --*/
  755. {
  756. UINTN Length;
  757. PCK_OBJECT Object;
  758. CHAR String[CK_MAX_NAME + 30];
  759. Object = CK_AS_OBJECT(Arguments[0]);
  760. Length = snprintf(String,
  761. sizeof(String),
  762. "<%s at 0x%p>",
  763. Object->Class->Name->Value,
  764. Object);
  765. String[sizeof(String) - 1] = '\0';
  766. Arguments[0] = CkpStringCreate(Vm, String, Length);
  767. return TRUE;
  768. }
  769. BOOL
  770. CkpObjectGet (
  771. PCK_VM Vm,
  772. PCK_VALUE Arguments
  773. )
  774. /*++
  775. Routine Description:
  776. This routine implements the default object get method.
  777. Arguments:
  778. Vm - Supplies a pointer to the virtual machine.
  779. Arguments - Supplies the function arguments.
  780. Return Value:
  781. TRUE on success.
  782. FALSE if execution caused a runtime error.
  783. --*/
  784. {
  785. PCK_CLASS Class;
  786. PCK_DICT Dict;
  787. PCK_INSTANCE Instance;
  788. if (!CK_IS_INSTANCE(Arguments[0])) {
  789. Class = CkpGetClass(Vm, Arguments[0]);
  790. CkpRuntimeError(Vm,
  791. "TypeError",
  792. "%s does not implement __get",
  793. Class->Name->Value);
  794. return FALSE;
  795. }
  796. Instance = CK_AS_INSTANCE(Arguments[0]);
  797. if (!CK_IS_NULL(Instance->Fields[0])) {
  798. Dict = CK_AS_DICT(Instance->Fields[0]);
  799. Arguments[0] = CkpDictGet(Dict, Arguments[1]);
  800. } else {
  801. Arguments[0] = CkUndefinedValue;
  802. }
  803. if (CK_IS_UNDEFINED(Arguments[0])) {
  804. CkpRuntimeError(Vm, "KeyError", "Key is not defined");
  805. return FALSE;
  806. }
  807. return TRUE;
  808. }
  809. BOOL
  810. CkpObjectSet (
  811. PCK_VM Vm,
  812. PCK_VALUE Arguments
  813. )
  814. /*++
  815. Routine Description:
  816. This routine implements the default object set method.
  817. Arguments:
  818. Vm - Supplies a pointer to the virtual machine.
  819. Arguments - Supplies the function arguments.
  820. Return Value:
  821. TRUE on success.
  822. FALSE if execution caused a runtime error.
  823. --*/
  824. {
  825. PCK_CLASS Class;
  826. PCK_DICT Dict;
  827. PCK_INSTANCE Instance;
  828. if (!CK_IS_INSTANCE(Arguments[0])) {
  829. Class = CkpGetClass(Vm, Arguments[0]);
  830. CkpRuntimeError(Vm,
  831. "TypeError",
  832. "%s does not implement __set",
  833. Class->Name->Value);
  834. return FALSE;
  835. }
  836. Instance = CK_AS_INSTANCE(Arguments[0]);
  837. if (CK_IS_NULL(Instance->Fields[0])) {
  838. Dict = CkpDictCreate(Vm);
  839. if (Dict == NULL) {
  840. return FALSE;
  841. }
  842. CK_OBJECT_VALUE(Instance->Fields[0], Dict);
  843. } else {
  844. Dict = CK_AS_DICT(Instance->Fields[0]);
  845. }
  846. CkpDictSet(Vm, Dict, Arguments[1], Arguments[2]);
  847. return TRUE;
  848. }
  849. BOOL
  850. CkpObjectImplements (
  851. PCK_VM Vm,
  852. PCK_VALUE Arguments
  853. )
  854. /*++
  855. Routine Description:
  856. This routine determines if the given object implements the given method.
  857. Arguments:
  858. Vm - Supplies a pointer to the virtual machine.
  859. Arguments - Supplies the function arguments.
  860. Return Value:
  861. TRUE on success.
  862. FALSE if execution caused a runtime error.
  863. --*/
  864. {
  865. CHAR Buffer[CK_MAX_METHOD_SIGNATURE];
  866. UINTN BufferSize;
  867. PCK_CLASS Class;
  868. CK_STRING FakeString;
  869. PCK_STRING NameString;
  870. CK_FUNCTION_SIGNATURE Signature;
  871. CK_VALUE Value;
  872. Class = CkpGetClass(Vm, Arguments[0]);
  873. if (!CK_IS_STRING(Arguments[1])) {
  874. CkpRuntimeError(Vm, "TypeError", "Expected a string");
  875. return FALSE;
  876. }
  877. NameString = CK_AS_STRING(Arguments[1]);
  878. if (!CK_IS_INTEGER(Arguments[2])) {
  879. CkpRuntimeError(Vm, "TypeError", "Expected an integer");
  880. return FALSE;
  881. }
  882. Signature.Name = NameString->Value;
  883. Signature.Length = NameString->Length;
  884. Signature.Arity = CK_AS_INTEGER(Arguments[2]);
  885. BufferSize = sizeof(Buffer);
  886. CkpPrintSignature(&Signature, Buffer, &BufferSize);
  887. Value = CkpStringFake(&FakeString, Buffer, BufferSize);
  888. if (!CK_IS_UNDEFINED(CkpDictGet(Class->Methods, Value))) {
  889. Arguments[0] = CkOneValue;
  890. } else {
  891. Arguments[0] = CkZeroValue;
  892. }
  893. return TRUE;
  894. }
  895. BOOL
  896. CkpObjectType (
  897. PCK_VM Vm,
  898. PCK_VALUE Arguments
  899. )
  900. /*++
  901. Routine Description:
  902. This routine returns the class of the given object.
  903. Arguments:
  904. Vm - Supplies a pointer to the virtual machine.
  905. Arguments - Supplies the function arguments.
  906. Return Value:
  907. TRUE on success.
  908. FALSE if execution caused a runtime error.
  909. --*/
  910. {
  911. PCK_CLASS Class;
  912. CK_VALUE Value;
  913. Class = CkpGetClass(Vm, Arguments[0]);
  914. CK_OBJECT_VALUE(Value, Class);
  915. Arguments[0] = Value;
  916. return TRUE;
  917. }
  918. BOOL
  919. CkpObjectMetaSame (
  920. PCK_VM Vm,
  921. PCK_VALUE Arguments
  922. )
  923. /*++
  924. Routine Description:
  925. This routine determines if two objects passed in as arguments are equal.
  926. Arguments:
  927. Vm - Supplies a pointer to the virtual machine.
  928. Arguments - Supplies the function arguments.
  929. Return Value:
  930. TRUE on success.
  931. FALSE if execution caused a runtime error.
  932. --*/
  933. {
  934. if (CkpAreValuesEqual(Arguments[1], Arguments[2])) {
  935. Arguments[0] = CkOneValue;
  936. } else {
  937. Arguments[0] = CkZeroValue;
  938. }
  939. return TRUE;
  940. }
  941. BOOL
  942. CkpClassName (
  943. PCK_VM Vm,
  944. PCK_VALUE Arguments
  945. )
  946. /*++
  947. Routine Description:
  948. This routine returns the name of the class object.
  949. Arguments:
  950. Vm - Supplies a pointer to the virtual machine.
  951. Arguments - Supplies the function arguments.
  952. Return Value:
  953. TRUE on success.
  954. FALSE if execution caused a runtime error.
  955. --*/
  956. {
  957. PCK_CLASS Class;
  958. Class = CK_AS_CLASS(Arguments[0]);
  959. CK_OBJECT_VALUE(Arguments[0], Class->Name);
  960. return TRUE;
  961. }
  962. BOOL
  963. CkpClassSuper (
  964. PCK_VM Vm,
  965. PCK_VALUE Arguments
  966. )
  967. /*++
  968. Routine Description:
  969. This routine returns the superclass of the given class object.
  970. Arguments:
  971. Vm - Supplies a pointer to the virtual machine.
  972. Arguments - Supplies the function arguments.
  973. Return Value:
  974. TRUE on success.
  975. FALSE if execution caused a runtime error.
  976. --*/
  977. {
  978. PCK_CLASS Class;
  979. Class = CK_AS_CLASS(Arguments[0]);
  980. CK_OBJECT_VALUE(Arguments[0], Class->Super);
  981. return TRUE;
  982. }
  983. BOOL
  984. CkpNullLogicalNot (
  985. PCK_VM Vm,
  986. PCK_VALUE Arguments
  987. )
  988. /*++
  989. Routine Description:
  990. This routine determines the logical not of a null class instance.
  991. Arguments:
  992. Vm - Supplies a pointer to the virtual machine.
  993. Arguments - Supplies the function arguments.
  994. Return Value:
  995. TRUE on success.
  996. FALSE if execution caused a runtime error.
  997. --*/
  998. {
  999. Arguments[0] = CkOneValue;
  1000. return TRUE;
  1001. }
  1002. BOOL
  1003. CkpNullToString (
  1004. PCK_VM Vm,
  1005. PCK_VALUE Arguments
  1006. )
  1007. /*++
  1008. Routine Description:
  1009. This routine converts a null instance into a string.
  1010. Arguments:
  1011. Vm - Supplies a pointer to the virtual machine.
  1012. Arguments - Supplies the function arguments.
  1013. Return Value:
  1014. TRUE on success.
  1015. FALSE if execution caused a runtime error.
  1016. --*/
  1017. {
  1018. Arguments[0] = CkpStringCreate(Vm, "null", 4);
  1019. return TRUE;
  1020. }
  1021. BOOL
  1022. CkpFunctionArity (
  1023. PCK_VM Vm,
  1024. PCK_VALUE Arguments
  1025. )
  1026. /*++
  1027. Routine Description:
  1028. This routine returns the number of argument required to pass to the given
  1029. function.
  1030. Arguments:
  1031. Vm - Supplies a pointer to the virtual machine.
  1032. Arguments - Supplies the function arguments.
  1033. Return Value:
  1034. TRUE on success.
  1035. FALSE if execution caused a runtime error.
  1036. --*/
  1037. {
  1038. CK_ARITY Arity;
  1039. PCK_CLOSURE Closure;
  1040. CK_ASSERT(CK_IS_CLOSURE(Arguments[0]));
  1041. Closure = CK_AS_CLOSURE(Arguments[0]);
  1042. Arity = CkpGetFunctionArity(Closure);
  1043. CK_INT_VALUE(Arguments[0], Arity);
  1044. return TRUE;
  1045. }
  1046. BOOL
  1047. CkpFunctionModule (
  1048. PCK_VM Vm,
  1049. PCK_VALUE Arguments
  1050. )
  1051. /*++
  1052. Routine Description:
  1053. This routine returns the module the function was defined in.
  1054. Arguments:
  1055. Vm - Supplies a pointer to the virtual machine.
  1056. Arguments - Supplies the function arguments.
  1057. Return Value:
  1058. TRUE on success.
  1059. FALSE if execution caused a runtime error.
  1060. --*/
  1061. {
  1062. PCK_CLOSURE Closure;
  1063. PCK_MODULE Module;
  1064. CK_ASSERT(CK_IS_CLOSURE(Arguments[0]));
  1065. Closure = CK_AS_CLOSURE(Arguments[0]);
  1066. switch (Closure->Type) {
  1067. case CkClosureBlock:
  1068. Module = Closure->U.Block.Function->Module;
  1069. break;
  1070. case CkClosurePrimitive:
  1071. Module = CkpModuleGet(Vm, CK_NULL_VALUE);
  1072. break;
  1073. case CkClosureForeign:
  1074. Module = Closure->U.Foreign.Module;
  1075. break;
  1076. default:
  1077. CK_ASSERT(FALSE);
  1078. Module = NULL;
  1079. break;
  1080. }
  1081. CK_OBJECT_VALUE(Arguments[0], Module);
  1082. return TRUE;
  1083. }
  1084. BOOL
  1085. CkpFunctionStackUsage (
  1086. PCK_VM Vm,
  1087. PCK_VALUE Arguments
  1088. )
  1089. /*++
  1090. Routine Description:
  1091. This routine returns the amount of stack a given function takes.
  1092. Arguments:
  1093. Vm - Supplies a pointer to the virtual machine.
  1094. Arguments - Supplies the function arguments.
  1095. Return Value:
  1096. TRUE on success.
  1097. FALSE if execution caused a runtime error.
  1098. --*/
  1099. {
  1100. PCK_CLOSURE Closure;
  1101. CK_SYMBOL_INDEX MaxStack;
  1102. CK_ASSERT(CK_IS_CLOSURE(Arguments[0]));
  1103. Closure = CK_AS_CLOSURE(Arguments[0]);
  1104. switch (Closure->Type) {
  1105. case CkClosureBlock:
  1106. MaxStack = Closure->U.Block.Function->MaxStack;
  1107. break;
  1108. case CkClosurePrimitive:
  1109. case CkClosureForeign:
  1110. MaxStack = 0;
  1111. break;
  1112. default:
  1113. CK_ASSERT(FALSE);
  1114. MaxStack = 0;
  1115. break;
  1116. }
  1117. CK_INT_VALUE(Arguments[0], MaxStack);
  1118. return TRUE;
  1119. }
  1120. BOOL
  1121. CkpCoreGarbageCollect (
  1122. PCK_VM Vm,
  1123. PCK_VALUE Arguments
  1124. )
  1125. /*++
  1126. Routine Description:
  1127. This routine implements the primitive to activate garbage collection.
  1128. Arguments:
  1129. Vm - Supplies a pointer to the virtual machine.
  1130. Arguments - Supplies the function arguments.
  1131. Return Value:
  1132. TRUE on success.
  1133. FALSE if execution caused a runtime error.
  1134. --*/
  1135. {
  1136. CkCollectGarbage(Vm);
  1137. return TRUE;
  1138. }
  1139. BOOL
  1140. CkpCoreImportModule (
  1141. PCK_VM Vm,
  1142. PCK_VALUE Arguments
  1143. )
  1144. /*++
  1145. Routine Description:
  1146. This routine implements the primitive to activate garbage collection.
  1147. Arguments:
  1148. Vm - Supplies a pointer to the virtual machine.
  1149. Arguments - Supplies the function arguments.
  1150. Return Value:
  1151. TRUE on success.
  1152. FALSE if execution caused a runtime error.
  1153. --*/
  1154. {
  1155. CK_VALUE Result;
  1156. UINTN StackIndex;
  1157. StackIndex = Arguments - Vm->Fiber->Stack;
  1158. if (!CK_IS_STRING(Arguments[1])) {
  1159. CkpRuntimeError(Vm, "TypeError", "Expected a string");
  1160. return FALSE;
  1161. }
  1162. //
  1163. // Don't save the return value directly on the stack yet, as the stack
  1164. // might get reallocated during the module load.
  1165. //
  1166. Result = CkpModuleLoad(Vm, Arguments[1], NULL);
  1167. if (CK_IS_NULL(Result)) {
  1168. return FALSE;
  1169. }
  1170. Vm->Fiber->Stack[StackIndex] = Result;
  1171. return TRUE;
  1172. }
  1173. BOOL
  1174. CkpCoreWrite (
  1175. PCK_VM Vm,
  1176. PCK_VALUE Arguments
  1177. )
  1178. /*++
  1179. Routine Description:
  1180. This routine writes a string to the interpreter output. It's possible no
  1181. output is wired up, in which case this is a no-op.
  1182. Arguments:
  1183. Vm - Supplies a pointer to the virtual machine.
  1184. Arguments - Supplies the function arguments.
  1185. Return Value:
  1186. TRUE on success.
  1187. FALSE if execution caused a runtime error.
  1188. --*/
  1189. {
  1190. PCK_STRING String;
  1191. if (!CK_IS_STRING(Arguments[1])) {
  1192. CkpRuntimeError(Vm, "TypeError", "Expected a string");
  1193. return FALSE;
  1194. }
  1195. String = CK_AS_STRING(Arguments[1]);
  1196. if (Vm->Configuration.Write != NULL) {
  1197. Vm->Configuration.Write(Vm, String->Value);
  1198. }
  1199. return TRUE;
  1200. }
  1201. BOOL
  1202. CkpCoreGetModules (
  1203. PCK_VM Vm,
  1204. PCK_VALUE Arguments
  1205. )
  1206. /*++
  1207. Routine Description:
  1208. This routine returns the modules dictionary.
  1209. Arguments:
  1210. Vm - Supplies a pointer to the virtual machine.
  1211. Arguments - Supplies the function arguments.
  1212. Return Value:
  1213. TRUE on success.
  1214. FALSE if execution caused a runtime error.
  1215. --*/
  1216. {
  1217. CK_OBJECT_VALUE(Arguments[0], Vm->Modules);
  1218. return TRUE;
  1219. }
  1220. BOOL
  1221. CkpCoreGetModulePath (
  1222. PCK_VM Vm,
  1223. PCK_VALUE Arguments
  1224. )
  1225. /*++
  1226. Routine Description:
  1227. This routine returns the current module path.
  1228. Arguments:
  1229. Vm - Supplies a pointer to the virtual machine.
  1230. Arguments - Supplies the function arguments.
  1231. Return Value:
  1232. TRUE on success.
  1233. FALSE if execution caused a runtime error.
  1234. --*/
  1235. {
  1236. if (Vm->ModulePath == NULL) {
  1237. Vm->ModulePath = CkpListCreate(Vm, 0);
  1238. }
  1239. if (Vm->ModulePath != NULL) {
  1240. CK_OBJECT_VALUE(Arguments[0], Vm->ModulePath);
  1241. } else {
  1242. Arguments[0] = CkNullValue;
  1243. }
  1244. return TRUE;
  1245. }
  1246. BOOL
  1247. CkpCoreSetModulePath (
  1248. PCK_VM Vm,
  1249. PCK_VALUE Arguments
  1250. )
  1251. /*++
  1252. Routine Description:
  1253. This routine sets the current module path.
  1254. Arguments:
  1255. Vm - Supplies a pointer to the virtual machine.
  1256. Arguments - Supplies the function arguments.
  1257. Return Value:
  1258. TRUE on success.
  1259. FALSE if execution caused a runtime error.
  1260. --*/
  1261. {
  1262. if (!CK_IS_LIST(Arguments[1])) {
  1263. CkpRuntimeError(Vm, "TypeError", "Expected a list");
  1264. return FALSE;
  1265. }
  1266. Vm->ModulePath = CK_AS_LIST(Arguments[1]);
  1267. return TRUE;
  1268. }
  1269. BOOL
  1270. CkpCoreRaise (
  1271. PCK_VM Vm,
  1272. PCK_VALUE Arguments
  1273. )
  1274. /*++
  1275. Routine Description:
  1276. This routine raises an exception.
  1277. Arguments:
  1278. Vm - Supplies a pointer to the virtual machine.
  1279. Arguments - Supplies the function arguments.
  1280. Return Value:
  1281. FALSE always to indicate an exception was raised.
  1282. --*/
  1283. {
  1284. if (!CkpObjectIsClass(CkpGetClass(Vm, Arguments[1]), Vm->Class.Exception)) {
  1285. CkpRuntimeError(Vm, "TypeError", "Expected an Exception");
  1286. } else {
  1287. CkpRaiseException(Vm, Arguments[1], 1);
  1288. }
  1289. return FALSE;
  1290. }
  1291. BOOL
  1292. CkpCoreImportAllSymbols (
  1293. PCK_VM Vm,
  1294. PCK_VALUE Arguments
  1295. )
  1296. /*++
  1297. Routine Description:
  1298. This routine imports all module level symbols from the given module.
  1299. Arguments:
  1300. Vm - Supplies a pointer to the virtual machine.
  1301. Arguments - Supplies the function arguments.
  1302. Return Value:
  1303. TRUE on success.
  1304. FALSE if execution caused a runtime error.
  1305. --*/
  1306. {
  1307. PCK_CLOSURE Closure;
  1308. PCK_MODULE CurrentModule;
  1309. PCK_FIBER Fiber;
  1310. PCK_CALL_FRAME Frame;
  1311. CK_SYMBOL_INDEX Index;
  1312. PCK_MODULE Module;
  1313. PCK_STRING String;
  1314. if (!CK_IS_MODULE(Arguments[1])) {
  1315. CkpRuntimeError(Vm, "TypeError", "Expected a module");
  1316. return FALSE;
  1317. }
  1318. Fiber = Vm->Fiber;
  1319. CK_ASSERT(Fiber->FrameCount != 0);
  1320. Frame = &(Fiber->Frames[Fiber->FrameCount - 1]);
  1321. Closure = Frame->Closure;
  1322. CK_ASSERT(Closure->Type == CkClosureBlock);
  1323. CurrentModule = Closure->U.Block.Function->Module;
  1324. Module = CK_AS_MODULE(Arguments[1]);
  1325. for (Index = 0; Index < Module->Variables.Count; Index += 1) {
  1326. String = CK_AS_STRING(Module->VariableNames.List.Data[Index]);
  1327. //
  1328. // Import everything that does not start with an underscore.
  1329. //
  1330. if ((String->Length >= 1) && (String->Value[0] != '_')) {
  1331. CkpDefineModuleVariable(Vm,
  1332. CurrentModule,
  1333. String->Value,
  1334. String->Length,
  1335. Module->Variables.Data[Index]);
  1336. }
  1337. }
  1338. return TRUE;
  1339. }