-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathruntime.js
1403 lines (1183 loc) · 38 KB
/
runtime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* EdgeLisp: A Lisp that compiles to JavaScript.
Copyright (C) 2008-2011 by Manuel Simoni.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Lisp runtime: this file contains all types and functions needed to
run compiled Lisp code.
Lisp code that uses functions like `eval' and `macroexpand' will
need to include the compiler, too.
This file has grown in a haphazard way, and is in serious need of a
cleanup. Also, it contains some data types and functions that
should probably be moved back into the compiler. */
/* Determines into which namespace the compiler puts identifiers in
the operator (first) position of a compound form. "function" for
Lisp-2, "variable" for Lisp-1. */
var lisp_operator_namespace = "function";
function lisp_bif_set_operator_namespace(namespace)
{
lisp_assert_string(namespace, "Bad operator namespace", namespace);
lisp_operator_namespace = namespace;
}
/*** Fasls ***/
// Fast-loadable, contains compiled JS code for a Lisp unit.
function Lisp_fasl(times)
{
// Maps time names ("execute", "compile") to compiled JS.
this.times = times;
}
// Printing a FASL prints its code.
Lisp_fasl.prototype.toString = function()
{
var res = "\n";
var this_times = this.times;
lisp_iter_dict(this_times, function(time) {
res += (time + ":\n" + this_times[time] + "\n");
});
return res;
}
// Creates an empty FASL.
function lisp_bif_make_fasl(_key_)
{
return new Lisp_fasl({});
}
function lisp_bif_load_fasl(_key_, fasl, time)
{
return lisp_load_fasl(fasl, time);
}
// Evaluates the code stored in a fasl for a certain time, such as
// "execute" or "compile".
function lisp_load_fasl(fasl, time)
{
lisp_assert_not_null(fasl.times[time]);
return eval(fasl.times[time]);
}
function lisp_bif_fasl_to_data_uri(_key_, fasl, time)
{
return "data:text/javascript," + escape(fasl.times[time]);
}
/*** Compiler identifiers ***/
/* A compiler identifier (CID) is the fully explicit form of
identifier used inside the compiler.
The name is a string.
The namespace is "variable", "function", "class" or another of the
Lisp-Omega namespaces.
The hygiene-context is null for user-entered identifiers, and a
UUID string for macro-generated identifiers. */
function Lisp_cid(name, namespace, hygiene_context)
{
lisp_assert_string(name, "Bad cid name", name);
lisp_assert_string(namespace, "Bad cid namespace", namespace);
this.name = name;
this.namespace = namespace;
this.hygiene_context = hygiene_context;
}
function lisp_identifier_to_cid(form, namespace)
{
lisp_assert_identifier_form(form, "bad variable identifier", form);
lisp_assert_string(namespace);
return new Lisp_cid(form.name, namespace, form.hygiene_context);
}
/* Turns a plain identifier like #'foo, or a compound identifier like
#'(%%identifier foo variable), or a macro like #'(function foo)
into a CID. */
function lisp_generalized_identifier_to_cid(form, default_namespace)
{
var ns = default_namespace ? default_namespace : "variable";
switch(form.formt) {
case "identifier":
return new Lisp_cid(form.name, ns, form.hygiene_context);
case "compound":
var exp = lisp_macroexpand(form);
lisp_assert_identifier_form(exp.elts[0]);
lisp_assert(exp.elts[0].name, "%%identifier");
var name = lisp_assert_identifier_form(exp.elts[1]);
var namespace = lisp_assert_identifier_form(exp.elts[2]);
return new Lisp_cid(name.name, namespace.name, name.hygiene_context);
}
lisp_error("Bad generalized identifier form", form);
}
function lisp_mangle_cid(cid)
{
return lisp_mangle(cid.name, cid.namespace, cid.hygiene_context);
}
function lisp_apropos(str)
{
var results = [];
lisp_iter_dict(lisp_globals, function(k) {
var cid = lisp_globals[k];
if (cid.name.indexOf(str) !== -1)
results.push(cid.name);
});
return results;
}
function lisp_bif_apropos(_key_, str)
{
return lisp_apropos(str);
}
/*** Literal ***/
function Lisp_literal()
{
}
/*** Nil ***/
function Lisp_nil()
{
}
/*** Numbers ***/
function lisp_number(numrepr)
{
return jsnums.fromString(numrepr);
}
function lisp_bif_string_to_number(_key_, s)
{
return lisp_number(s);
}
function Lisp_number()
{
}
function Lisp_integer()
{
}
/*** Functions ***/
function lisp_bif_fast_apply(_key_, fun, _arguments)
{
return fun.apply(null, _arguments);
}
/*** Forms ***/
function Lisp_form()
{
}
function Lisp_number_form(sign, integral_digits, fractional_digits)
{
this.formt = "number";
this.sign = sign;
this.integral_digits = integral_digits;
this.fractional_digits = fractional_digits;
}
Lisp_number_form.prototype.toString = function() {
var sign_str = this.sign === "+" ? "" : "-";
return sign_str + this.integral_digits + this.fractional_digits;
}
function Lisp_string_form(s)
{
this.formt = "string";
this.s = s;
}
Lisp_string_form.prototype.toString = function() {
return "\"" + this.s + "\"";
}
function Lisp_identifier_form(name, hygiene_context)
{
this.formt = "identifier";
this.name = name;
this.hygiene_context = hygiene_context;
}
function lisp_bif_literal_identifier_equalp(_key_, a, b)
{
return lisp_literal_identifier_equalp(a, b);
}
function lisp_literal_identifier_equalp(a, b)
{
lisp_assert_identifier_form(a);
lisp_assert_identifier_form(b);
return a.name === b.name;
}
Lisp_identifier_form.prototype.toString = function() {
return this.name + (this.hygiene_context ? "\\" + this.hygiene_context : "");
}
function Lisp_compound_form(elts)
{
this.formt = "compound";
this.elts = elts;
}
Lisp_compound_form.prototype.toString = function() {
return "(" + this.elts.map(function(elt){return elt.toString();}).join(" ") + ")";
}
/* This whole comments-as-forms business is silly, but atm I don't
know another way to have comments ignored. */
function Lisp_comment_form(contents)
{
this.formt = "comment";
this.contents = contents;
}
/*** Utilities for generated code. ***/
function lisp_undefined_identifier(name, namespace, hygiene_context)
{
return _lisp_function_undefined_identifier(null, name, namespace,
(hygiene_context ? hygiene_context : null));
}
function _lisp_function_undefined_identifier(_key_, name, namespace, hygiene_context)
{
return lisp_error("Undefined " + namespace, name +
(hygiene_context ? ("\\"+hygiene_context) : ""));
}
/* Used inside lambdas. */
function lisp_arity_min(length, min)
{
if (length < min)
return lisp_error("Too few arguments", lisp_arity_min.caller);
}
function lisp_arity_min_max(length, min, max)
{
if (length < min)
return lisp_error("Too few arguments", lisp_arity_min_max.caller);
if (length > max)
return lisp_error("Too many arguments", lisp_arity_min_max.caller);
}
/* Returns the sequence of arguments to which the rest parameter is
bound; called with a function's arguments and the count of
positional (required and optional) parameters of the function. */
function lisp_rest_param(_arguments, max) {
var args = [];
var offset = 1 + max; // Skip calling convention argument.
var len = _arguments.length;
for (var i = offset; i < len; i++) {
args[i - offset] = _arguments[i];
}
return args;
}
/* Used inside lambdas for checking arguments against their types, if any. */
function lisp_check_type(obj, type)
{
if (!lisp_subclassp(lisp_class_of(obj), type))
return lisp_error("Type error", [obj, lisp_show(type)]);
}
/* This later replaced with generic function in boot.lisp. */
function lisp_show(obj)
{
return _lisp_function_show(null, obj);
}
function _lisp_function_show(_key_, obj)
{
return JSON.stringify(obj);
}
function lisp_array_contains(array, elt)
{
for (var i = 0; i < array.length; i++) {
if (array[i] === elt) return true;
}
return false;
}
function lisp_iter_dict(dict, fun)
{
for (var k in dict) {
if (dict.hasOwnProperty(k))
fun(k);
}
}
function lisp_lists_equal(list1, list2)
{
if (list1.length !== list2.length) return false;
for (var i = 0, len = list1.length; i < len; i++) {
if (list1[i] !== list2[i]) return false;
}
return true;
}
function Lisp_error()
{
}
function Lisp_runtime_error(message, arg)
{
this.message = message;
this.arg = arg;
}
function lisp_bif_runtime_error_message(_key_, runtime_error)
{
return runtime_error.message;
}
function lisp_bif_runtime_error_arg(_key_, runtime_error)
{
return runtime_error.arg;
}
function lisp_error(message, arg)
{
return lisp_signal(new Lisp_runtime_error(message, arg));
}
function lisp_signal(condition)
{
// careful, maybe Lisp SIGNAL isn't defined yet
if (typeof _lisp_function_signal !== "undefined")
return _lisp_function_signal(null, condition);
else
throw condition;
}
function lisp_assert(value, message, arg)
{
if (!value) return lisp_error(message, arg);
return value;
}
function lisp_assert_not_null(value, message, arg)
{
lisp_assert(value != null, message, arg);
return value;
}
function lisp_assert_number(value, message, arg)
{
lisp_assert(typeof value === "number", message, arg);
return value;
}
function lisp_assert_string(value, message, arg)
{
lisp_assert(typeof value === "string", message, arg);
return value;
}
function lisp_assert_nonempty_string(value, message, arg)
{
lisp_assert_string(value, message, arg);
lisp_assert(value.length > 0, message, arg);
return value;
}
function lisp_assert_function(value, message, arg)
{
lisp_assert(typeof value === "function", message, arg);
return value;
}
function lisp_assert_list(value, message, arg)
{
lisp_assert(value.length !== undefined, message, arg);
return value;
}
function lisp_assert_identifier_form(value, message, arg)
{
lisp_assert(typeof value === "object", message, arg);
lisp_assert(value.formt === "identifier", message, arg);
lisp_assert_nonempty_string(value.name, message, arg);
return value;
}
function lisp_assert_string_form(value, message, arg)
{
lisp_assert(typeof value === "object", message, arg);
lisp_assert(value.formt === "string", message, arg);
lisp_assert_string(value.s, message, arg);
return value;
}
function lisp_assert_number_form(value, message, arg)
{
lisp_assert(typeof value === "object", message, arg);
lisp_assert(value.formt === "number", message, arg);
lisp_assert_string(value.sign, message, arg);
lisp_assert_string(value.integral_digits, message, arg);
lisp_assert_string(value.fractional_digits, message, arg);
return value;
}
function lisp_assert_compound_form(value, message, arg)
{
lisp_assert(typeof value === "object", message, arg);
lisp_assert(value.formt === "compound", message, arg);
lisp_assert_not_null(value.elts, message, arg);
lisp_assert_not_null(value.elts.length, message, arg);
return value;
}
/*** Built-in form manipulation functions ***/
/* Applies a Lisp function to the elements of a compound form, a
simple form of destructuring. The form's elements are simply
supplied as the function's positional arguments. */
function lisp_bif_compound_apply(_key_, fun, form)
{
var _key_ = null;
var args = [ _key_ ].concat(form.elts);
var thisArg = null;
return fun.apply(thisArg, args);
}
/* Adds form at end of compound form. */
function lisp_bif_compound_add(_key_, compound, form)
{
compound.elts.push(form);
return compound;
}
/* Creates a compound form from all positional arguments, which must
be forms. */
function lisp_bif_make_compound(_key_)
{
var elts = [];
for (var i = 1; i < arguments.length; i++) {
var elt = arguments[i];
lisp_assert(elt && elt.formt, "make-compound", elt);
elts = elts.concat(elt);
}
return new Lisp_compound_form(elts);
}
/* Creates a compound form by appending all positional arguments,
which must be compound forms or lists. This fuzzyness in accepting
both compound forms and lists enables the splicing in of forms
supplied via the rest parameter. */
function lisp_bif_append_compounds(_key_)
{
var elts = [];
for (var i = 1; i < arguments.length; i++) {
var elt = arguments[i];
if (elt.formt === "compound") {
elts = elts.concat(elt.elts);
} else {
lisp_assert(elt.length !== undefined, "append-compounds", elt);
elts = elts.concat(elt);
}
}
return new Lisp_compound_form(elts);
}
function lisp_bif_compound_map(_key_, fun, compound)
{
function js_fun(elt) {
return fun(null, elt);
}
lisp_assert_not_null(fun);
lisp_assert_compound_form(compound);
return new Lisp_compound_form(compound.elts.map(js_fun));
}
function lisp_bif_compound_elt(_key_, compound, i)
{
lisp_assert_compound_form(compound, "compound-elt", compound);
lisp_assert(i < compound.elts.length, "index too large", [compound, i]);
var elt = compound.elts[i];
return elt;
}
function lisp_bif_compound_len(_key_, compound)
{
lisp_assert_compound_form(compound, "compound-len", compound);
return compound.elts.length;
}
function lisp_bif_compound_elts(_key_, compound)
{
lisp_assert_compound_form(compound, "compound-elts", compound);
return compound.elts;
}
function lisp_bif_compound_slice(_key_, compound, start, end)
{
lisp_assert_compound_form(compound, "compound-slice", compound);
return new Lisp_compound_form(compound.elts.slice(start, end));
}
function lisp_bif_compound_emptyp(_key_, compound)
{
lisp_assert_compound_form(compound, "compound-empty?", compound);
return compound.elts.length === 0;
}
/*** Built-in string dictionaries ***/
/* This is the type of dictionaries used to hold keyword arguments,
i.e. the one a function with an `&all-keys' signature keyword
receives. They're called string dictionaries because their keys
can only be strings.
By convention, all keys are prefixed with "%" (but not transformed
like variables), so we never get in conflict with JS's special
names, such as "prototype". */
function lisp_mangle_string_dict_key(name)
{
lisp_assert_string(name, "bad string dict key", name);
return "%" + name;
}
function lisp_unmangle_string_dict_key(name)
{
return name.slice(1);
}
function lisp_is_string_dict_key(k)
{
return k[0] === "%";
}
function lisp_bif_string_dict_map(_key_, fun, dict)
{
lisp_iter_dict(dict, function(k) {
fun(null, lisp_unmangle_string_dict_key(k));
});
return null;
}
function Lisp_string_dict()
{
}
/* Turns an ordinary dictionary into a string dictionary. May only be
called if the caller has ensured that all keys are properly
mangled, or that the dictionary is empty. */
function lisp_fast_string_dict(js_dict)
{
js_dict.__proto__ = Lisp_string_dict.prototype;
return js_dict;
}
function lisp_bif_string_dict_get(_key_, dict, key)
{
var val = dict[lisp_mangle_string_dict_key(key)];
return val !== undefined ? val : "null";
}
function lisp_bif_string_dict_put(_key_, dict, key, value)
{
dict[lisp_mangle_string_dict_key(key)] = value;
return value;
}
function lisp_bif_string_dict_has_key(_key_, dict, key)
{
return lisp_mangle_string_dict_key(key) in dict;
}
/*** Control flow ***/
function lisp_bif_call_with_catch_tag(_key_, tag, body_fun)
{
try {
return body_fun(null);
} catch(obj) {
if (obj && (obj.lisp_tag === tag))
return obj.lisp_value;
else
throw obj;
}
}
function lisp_bif_throw(_key_, tag, value)
{
throw { lisp_tag: tag, lisp_value: (value !== undefined ? value : null) };
}
function lisp_bif_call_unwind_protected(_key_, protected_fun, cleanup_fun)
{
try {
return protected_fun(null);
} finally {
cleanup_fun(null);
}
}
function lisp_bif_call_forever(_key_, body_fun)
{
while(true)
body_fun(null);
}
/*** Multiple Dispatch ***/
function Lisp_generic(name)
{
this.method_entries = [];
this.name = name;
}
function lisp_generic_name(generic)
{
return generic.name ? generic.name : "anonymous generic";
}
function lisp_bif_generic_name(_key_, generic)
{
return lisp_generic_name(generic);
}
function Lisp_method_entry(method, specializers)
{
this.method = method;
this.specializers = specializers;
}
function lisp_bif_make_generic(_key_, name)
{
return new Lisp_generic(name);
}
function lisp_make_method_entry(method, specializers)
{
return new Lisp_method_entry(method, specializers);
}
function lisp_bif_put_method(_key_, generic, specializers, method)
{
for (var i = 0, len = generic.method_entries.length; i < len; i++) {
var me = generic.method_entries[i];
if (lisp_lists_equal(me.specializers, specializers)) {
me.method = method;
return null;
}
}
generic.method_entries.push(lisp_make_method_entry(method, specializers));
return null;
}
function lisp_bif_params_specializers(_key_, params)
{
if (!lisp_compile_sig)
lisp_error("Compiler not loaded");
var specializers = [];
// This empty compilation state is a HACK, but it doesn't matter,
// as we're only interested in the specializers, which are a
// purely syntactical matter.
var st = new Lisp_compilation_state();
var sig = lisp_compile_sig(st, params.elts);
for (var i = 0, len = sig.req_params.length; i < len; i++) {
var param = sig.req_params[i];
var specializer = param.specializer ? param.specializer : "object";
var specs = [ new Lisp_identifier_form("%%identifier"),
new Lisp_identifier_form(specializer, param.hygiene_context),
new Lisp_identifier_form("class") ];
specializers.push(new Lisp_compound_form(specs));
}
return new Lisp_compound_form(specializers);
}
function lisp_bif_find_method(_key_, generic, arguments)
{
var applicable_mes =
lisp_find_applicable_method_entries(generic, arguments);
if (applicable_mes.length === 0)
return lisp_no_applicable_method(generic, arguments);
var me = lisp_most_specific_method_entry(generic, applicable_mes);
if (me)
return me.method;
else
return lisp_no_most_specific_method(generic, arguments, applicable_mes);
}
function lisp_find_applicable_method_entries(generic, arguments)
{
var actual_specializers = [];
// start at 1 to skip over calling convention argument
for (var i = 1, len = arguments.length; i < len; i++)
actual_specializers.push(lisp_class_of(arguments[i]));
var applicable_mes = [];
var mes = generic.method_entries;
for (var i = 0, len = mes.length; i < len; i++) {
if (lisp_specializer_lists_agree(actual_specializers,
mes[i].specializers)) {
applicable_mes.push(mes[i]);
}
}
return applicable_mes;
}
function lisp_specializer_lists_agree(actuals, formals)
{
if (actuals.length != formals.length) return false;
for (var i = 0, len = actuals.length; i < len; i++)
if (!lisp_subclassp(actuals[i], formals[i]))
return false;
return true;
}
function lisp_most_specific_method_entry(generic, applicable_mes)
{
if (applicable_mes.length === 1)
return applicable_mes[0];
for (var i = 0, len = applicable_mes.length; i < len; i++)
if (lisp_least_method_entry(applicable_mes[i], applicable_mes))
return applicable_mes[i];
return null;
}
function lisp_least_method_entry(me, mes)
{
for (var i = 0, len = mes.length; i < len; i++) {
if (me === mes[i])
continue;
if (!lisp_smaller_method_entry(me, mes[i]))
return false;
}
return true;
}
function lisp_smaller_method_entry(me1, me2)
{
if (me1.specializers.length != me2.specializers.length)
return false;
for (var i = 0, len = me1.specializers.length; i < len; i++)
if ((!lisp_classes_comparable(me1.specializers[i],
me2.specializers[i])) ||
(!lisp_subclassp(me1.specializers[i],
me2.specializers[i])))
return false;
return true;
}
function lisp_classes_comparable(class1, class2)
{
return ((lisp_subclassp(class1, class2)) ||
(lisp_subclassp(class2, class1)))
}
function lisp_no_applicable_method(generic, arguments)
{
return _lisp_function_no_applicable_method(null, generic, arguments);
}
function _lisp_function_no_applicable_method(_key_, generic, arguments)
{
return lisp_error("No applicable method", lisp_generic_name(generic));
}
function lisp_no_most_specific_method(generic, arguments, applicable_mes)
{
return _lisp_function_no_most_specific_method(null, generic, arguments);
}
function _lisp_function_no_most_specific_method(_key_, generic, arguments)
{
return lisp_error("No most specific method", lisp_generic_name(generic));
}
/*** Classes ***/
function lisp_bif_class_name(_key_, klass)
{
return klass.lisp_name ? klass.lisp_name : "anonymous class";
}
function lisp_bif_set_class_name(_key_, klass, name)
{
klass.lisp_name = name;
return name;
}
function Lisp_native()
{
}
Lisp_native.prototype.toString = function()
{
return "JS object";
}
function lisp_class_of(obj)
{
if (obj === undefined)
return lisp_error("segfault");
else if (obj === null)
return Lisp_nil.prototype;
else if (obj.hasOwnProperty("lisp_is_class"))
return Lisp_class.prototype;
else {
var proto = obj.__proto__;
if (proto.hasOwnProperty("lisp_is_class"))
return proto;
else
return Lisp_native.prototype;
}
}
function lisp_bif_class_of(_key_, obj)
{
return lisp_class_of(obj);
}
function lisp_bif_the(_key_, klass, object)
{
if (lisp_subclassp(lisp_class_of(object), klass)) {
return object;
} else {
lisp_error(lisp_show(object) + " is not of expected type", lisp_show(klass));
}
}
function lisp_show_type_sensibly(type)
{
if (type === null) {
return "#![null-type (error)]";
} else if (type.lisp_name) {
return "#![" + type.lisp_name + "]";
} else {
return JSON.stringify(type);
}
}
/* Returns true iff type1 is a general subclass of type2, meaning
either equal to type2, or a subclass of type2. */
function lisp_subclassp(type1, type2)
{
if ((!type1) || (!type2))
lisp_error("subclass?", [lisp_show_type_sensibly(type1),
lisp_show_type_sensibly(type2)]);
if (type1 === type2)
return true;
var supertype = type1.lisp_superclass;
if (supertype)
return lisp_subclassp(supertype, type2);
return false;
}
function lisp_bif_subclassp(_key_, type1, type2)
{
return lisp_subclassp(type1, type2);
}
function Lisp_class()
{
/* Classes set this property, which identifies them to CLASS-OF as
such. The rationale is that we don't want to go ahead and muck
with the prototype structure of non-Lisp, built-in classes.
Lest they completely fall apart. */
this.lisp_is_class = true;
}
function lisp_bif_make_class(_key_)
{
return new Lisp_class();
}
function lisp_bif_set_superclass(_key_, clsA, clsB)
{
return lisp_set_superclass(clsA, clsB);
}
function lisp_bif_superclass(_key_, clsA)
{
return lisp_superclass(clsA);
}
function lisp_superclass(clsA)
{
return clsA.lisp_superclass ? clsA.lisp_superclass : lisp_error("no superclass", clsA);
}
function lisp_set_superclass(clsA, clsB)
{
clsA.lisp_superclass = clsB;
return clsB;
}
function lisp_bif_make_instance(_key_, cls)
{
var obj = new Object();
obj.__proto__ = cls;
return obj;
}
/*** Slots ***/
function lisp_bif_slot_value(_key_, obj, name)
{
lisp_assert_string(name);
var value = obj[lisp_mangle_slot(name)];
return value !== undefined ? value : null;
}
function lisp_bif_set_slot_value(_key_, obj, name, value)
{
lisp_assert_string(name);
obj[lisp_mangle_slot(name)] = value;
return value;
}
function lisp_bif_has_slot(_key_, obj, name)
{
lisp_assert_string(name);
return obj.hasOwnProperty(lisp_mangle_slot(name));
}
/*** Utilities ***/
/* Maps the mangled CIDs of macros to their expander functions.
This is defined, but unused in the runtime, so that users calling
macroexpand don't get an error. */
var lisp_macros_table = {};
function lisp_macro_function(cid)
{
var macro_function = lisp_macros_table[lisp_mangle_cid(cid)];
if (macro_function) {
return macro_function;
} else {
var unhygienic_cid = new Lisp_cid(cid.name,
cid.namespace,
null);
return lisp_macros_table[lisp_mangle_cid(unhygienic_cid)];
}
}
function lisp_set_macro_function(cid, expander)
{
var mangled_name = lisp_mangle_cid(cid);