File: internal.cpp

package info (click to toggle)
kdelibs 4:2.2.2-13.woody.14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 36,832 kB
  • ctags: 40,077
  • sloc: cpp: 313,284; ansic: 20,558; xml: 11,448; sh: 11,318; makefile: 2,426; perl: 2,084; yacc: 1,663; java: 1,538; lex: 629; python: 300
file content (914 lines) | stat: -rw-r--r-- 18,622 bytes parent folder | download
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
/*
 *  This file is part of the KDE libraries
 *  Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#include "internal.h"

#include <assert.h>
#include <stdio.h>

#include "kjs.h"
#include "object.h"
#include "types.h"
#include "operations.h"
#include "regexp.h"
#include "nodes.h"
#include "lexer.h"
#include "collector.h"
#include "debugger.h"

#define I18N_NOOP(s) s

extern int kjsyyparse();

using namespace KJS;

const TypeInfo UndefinedImp::info = { "Undefined", UndefinedType, 0, 0, 0 };
const TypeInfo NullImp::info = { "Null", NullType, 0, 0, 0 };
const TypeInfo NumberImp::info = { "Number", NumberType, 0, 0,0  };
const TypeInfo StringImp::info = { "String", StringType, 0, 0, 0 };
const TypeInfo BooleanImp::info = { "Boolean", BooleanType, 0, 0, 0 };
const TypeInfo CompletionImp::info = { "Completion", CompletionType, 0, 0, 0 };
const TypeInfo ReferenceImp::info = { "Reference", ReferenceType, 0, 0, 0 };

UndefinedImp *UndefinedImp::staticUndefined = 0;

UndefinedImp::UndefinedImp()
{
}

KJSO UndefinedImp::toPrimitive(Type) const
{
  return (Imp*)this;
}

Boolean UndefinedImp::toBoolean() const
{
  return Boolean(false);
}

Number UndefinedImp::toNumber() const
{
  return Number(NaN);
}

String UndefinedImp::toString() const
{
  return String("undefined");
}

Object UndefinedImp::toObject() const
{
  return Error::createObject(TypeError, I18N_NOOP("Undefined value"));
}

NullImp *NullImp::staticNull = 0;

NullImp::NullImp()
{
}

KJSO NullImp::toPrimitive(Type) const
{
  return (Imp*)this;
}

Boolean NullImp::toBoolean() const
{
  return Boolean(false);
}

Number NullImp::toNumber() const
{
  return Number(0);
}

String NullImp::toString() const
{
  return String("null");
}

Object NullImp::toObject() const
{
  return Error::createObject(TypeError, I18N_NOOP("Null value"));
}

BooleanImp* BooleanImp::staticTrue = 0;
BooleanImp* BooleanImp::staticFalse = 0;

KJSO BooleanImp::toPrimitive(Type) const
{
  return (Imp*)this;
}

Boolean BooleanImp::toBoolean() const
{
  return Boolean((BooleanImp*)this);
}

Number BooleanImp::toNumber() const
{
  return Number(val ? 1 : 0);
}

String BooleanImp::toString() const
{
  return String(val ? "true" : "false");
}

Object BooleanImp::toObject() const
{
  return Object::create(BooleanClass, Boolean((BooleanImp*)this));
}

NumberImp::NumberImp(double v)
  : val(v)
{
}

KJSO NumberImp::toPrimitive(Type) const
{
  return (Imp*)this;
}

Boolean NumberImp::toBoolean() const
{
  bool b = !((val == 0) /* || (iVal() == N0) */ || isNaN(val));

  return Boolean(b);
}

Number NumberImp::toNumber() const
{
  return Number((NumberImp*)this);
}

String NumberImp::toString() const
{
  return String(UString::from(val));
}

Object NumberImp::toObject() const
{
  return Object::create(NumberClass, Number((NumberImp*)this));
}

StringImp::StringImp(const UString& v)
  : val(v)
{
}

KJSO StringImp::toPrimitive(Type) const
{
  return (Imp*)this;
}

Boolean StringImp::toBoolean() const
{
  return Boolean(val.size() > 0);
}

Number StringImp::toNumber() const
{
  return Number(val.toDouble());
}

String StringImp::toString() const
{
  return String((StringImp*)this);
}

Object StringImp::toObject() const
{
  return Object::create(StringClass, String((StringImp*)this));
}

ReferenceImp::ReferenceImp(const KJSO& b, const UString& p)
  : base(b), prop(p)
{
}

void ReferenceImp::mark(Imp*)
{
  Imp::mark();
  Imp *im = base.imp();
  if (im && !im->marked())
    im->mark();
}

CompletionImp::CompletionImp(Compl c, const KJSO& v, const UString& t)
  : comp(c), val(v), tar(t)
{
}

void CompletionImp::mark(Imp*)
{
  Imp::mark();
  Imp *im = val.imp();
  if (im && !im->marked())
    im->mark();
}

RegExpImp::RegExpImp()
  : ObjectImp(RegExpClass), reg(0L)
{
}

RegExpImp::~RegExpImp()
{
  delete reg;
}

// ECMA 10.2
Context::Context(CodeType type, Context *callingContext,
		 FunctionImp *func, const List *args, Imp *thisV)
{
  Global glob(Global::current());

  // create and initialize activation object (ECMA 10.1.6)
  if (type == FunctionCode || type == AnonymousCode || type == HostCode) {
    activation = new ActivationImp(func, args);
    variable = activation;
  } else {
    activation = KJSO();
    variable = glob;
  }

  // ECMA 10.2
  switch(type) {
    case EvalCode:
      if (callingContext) {
	scopeChain = callingContext->copyOfChain();
	variable = callingContext->variableObject();
	thisVal = callingContext->thisValue();
	break;
      } // else same as GlobalCode
    case GlobalCode:
      scopeChain = new List();
      scopeChain->append(glob);
      thisVal = glob.imp();
      break;
    case FunctionCode:
    case AnonymousCode:
      if (type == FunctionCode) {
	scopeChain = ((DeclaredFunctionImp*)func)->scopeChain()->copy();
	scopeChain->prepend(activation);
      } else {
	scopeChain = new List();
	scopeChain->append(activation);
	scopeChain->append(glob);
      }
      variable = activation; /* TODO: DontDelete ? (ECMA 10.2.3) */
      if (thisV->type() >= ObjectType) {
	thisVal = thisV;
      }
      else
	thisVal = glob.imp();
      break;
    case HostCode:
      if (thisV->type() >= ObjectType)
	thisVal = thisV;
      else
	thisVal = glob;
      variable = activation; /* TODO: DontDelete (ECMA 10.2.4) */
      scopeChain = new List();
      scopeChain->append(activation);
      if (func->hasAttribute(ImplicitThis))
	scopeChain->append(KJSO(thisVal));
      if (func->hasAttribute(ImplicitParents)) {
	/* TODO ??? */
      }
      scopeChain->append(glob);
      break;
    }
}

Context::~Context()
{
  delete scopeChain;
}

Context *Context::current()
{
  return KJScriptImp::curr ? KJScriptImp::curr->con : 0L;
}

void Context::setCurrent(Context *c)
{
  KJScriptImp::current()->con = c;
}

void Context::pushScope(const KJSO &s)
{
  scopeChain->prepend(s);
}

void Context::popScope()
{
  scopeChain->removeFirst();
}

List* Context::copyOfChain()
{
  return scopeChain->copy();
}


AnonymousFunction::AnonymousFunction()
  : Function(0L)
{
  /* TODO */
}

DeclaredFunctionImp::DeclaredFunctionImp(const UString &n,
					 FunctionBodyNode *b, const List *sc)
  : ConstructorImp(n), body(b), scopes(sc->copy())
{
}

DeclaredFunctionImp::~DeclaredFunctionImp()
{
  delete scopes;
}

// step 2 of ECMA 13.2.1. rest in FunctionImp::executeCall()
Completion DeclaredFunctionImp::execute(const List &)
{
 /* TODO */

#ifdef KJS_DEBUGGER
  Debugger *dbg = KJScriptImp::current()->debugger();
  int oldSourceId = -1;
  if (dbg) {
    oldSourceId = dbg->sourceId();
    dbg->setSourceId(body->sourceId());
  }
#endif

  Completion result = body->execute();

#ifdef KJS_DEBUGGER
  if (dbg) {
    dbg->setSourceId(oldSourceId);
  }
#endif

  if (result.complType() == Throw || result.complType() == ReturnValue)
      return result;
  return Completion(Normal, Undefined()); /* TODO: or ReturnValue ? */
}

// ECMA 13.2.2 [[Construct]]
Object DeclaredFunctionImp::construct(const List &args)
{
  Object obj(ObjectClass);
  KJSO p = get("prototype");
  if (p.isObject())
    obj.setPrototype(p);
  else
    obj.setPrototype(Global::current().objectPrototype());

  KJSO res = executeCall(obj.imp(), &args);

  Object v = Object::dynamicCast(res);
  if (v.isNull())
    return obj;
  else
    return v;
}

Completion AnonymousFunction::execute(const List &)
{
 /* TODO */
  return Completion(Normal, Null());
}

// ECMA 10.1.8
class ArgumentsObject : public ObjectImp {
public:
  ArgumentsObject(FunctionImp *func, const List *args);
};

ArgumentsObject::ArgumentsObject(FunctionImp *func, const List *args)
  : ObjectImp(UndefClass)
{
  put("callee", Function(func), DontEnum);
  if (args) {
    put("length", Number(args->size()), DontEnum);
    ListIterator arg = args->begin();
    for (int i = 0; arg != args->end(); arg++, i++) {
      put(UString::from(i), *arg, DontEnum);
    }
  }
}

const TypeInfo ActivationImp::info = { "Activation", ActivationType, 0, 0, 0 };

// ECMA 10.1.6
ActivationImp::ActivationImp(FunctionImp *f, const List *args)
{
  KJSO aobj(new ArgumentsObject(f, args));
  put("arguments", aobj, DontDelete);
  /* TODO: this is here to get myFunc.arguments and myFunc.a1 going.
     I can't see this described in the spec but it's possible in browsers. */
  if (!f->name().isNull())
    f->put("arguments", aobj);
}

ExecutionStack::ExecutionStack()
  : progNode(0L), firstNode(0L), prev(0)
{
}

ExecutionStack* ExecutionStack::push()
{
  ExecutionStack *s = new ExecutionStack();
  s->prev = this;

  return s;
}

ExecutionStack* ExecutionStack::pop()
{
  ExecutionStack *s = prev;
  delete this;

  return s;
}

KJScriptImp* KJScriptImp::curr = 0L;
KJScriptImp* KJScriptImp::hook = 0L;
int          KJScriptImp::instances = 0;
int          KJScriptImp::running = 0;

KJScriptImp::KJScriptImp(KJScript *s)
  : scr(s),
    initialized(false),
    glob(0L),
#ifdef KJS_DEBUGGER
    dbg(0L),
#endif
    retVal(0L)
{
  instances++;
  KJScriptImp::curr = this;
  // are we the first interpreter instance ? Initialize some stuff
  if (instances == 1)
    globalInit();
  stack = new ExecutionStack();
  clearException();
  lex = new Lexer();
}

KJScriptImp::~KJScriptImp()
{
  KJScriptImp::curr = this;

#ifdef KJS_DEBUGGER
  attachDebugger(0L);
#endif

  clear();

  delete lex;
  lex = 0L;

  delete stack;
  stack = 0L;

  KJScriptImp::curr = 0L;
  // are we the last of our kind ? Free global stuff.
  if (instances == 1)
    globalClear();
  instances--;
}

void KJScriptImp::globalInit()
{
  UndefinedImp::staticUndefined = new UndefinedImp();
  UndefinedImp::staticUndefined->ref();
  NullImp::staticNull = new NullImp();
  NullImp::staticNull->ref();
  BooleanImp::staticTrue = new BooleanImp(true);
  BooleanImp::staticTrue->ref();
  BooleanImp::staticFalse = new BooleanImp(false);
  BooleanImp::staticFalse->ref();
}

void KJScriptImp::globalClear()
{
  UndefinedImp::staticUndefined->deref();
  UndefinedImp::staticUndefined = 0L;
  NullImp::staticNull->deref();
  NullImp::staticNull = 0L;
  BooleanImp::staticTrue->deref();
  BooleanImp::staticTrue = 0L;
  BooleanImp::staticFalse->deref();
  BooleanImp::staticFalse = 0L;
}

void KJScriptImp::mark()
{
  if (exVal && !exVal->marked())
    exVal->mark();
  if (retVal && !retVal->marked())
    retVal->mark();
  if (glob.imp())
    glob.imp()->mark();
  UndefinedImp::staticUndefined->mark();
  NullImp::staticNull->mark();
  BooleanImp::staticTrue->mark();
  BooleanImp::staticFalse->mark();
}

void KJScriptImp::init()
{
  KJScriptImp::curr = this;

  clearException();
  retVal = 0L;

  if (!initialized) {
    // add this interpreter to the global chain
    // as a root set for garbage collection
    if (hook) {
      prev = hook;
      next = hook->next;
      hook->next->prev = this;
      hook->next = this;
    } else {
      hook = next = prev = this;
    }

    glob.init();
    con = new Context();
    firstN = 0L;
    progN = 0L;
    recursion = 0;
    errMsg = "";
    initialized = true;
#ifdef KJS_DEBUGGER
    sid = -1;
#endif
  }
}

void KJScriptImp::clear()
{
  if ( recursion ) {
#ifndef NDEBUG
      fprintf(stderr, "KJS: ignoring clear() while running\n");
#endif
      return;
  }
  KJScriptImp *old = curr;
  if (initialized) {
    KJScriptImp::curr = this;

    Node::setFirstNode(firstNode());
    Node::deleteAllNodes();
    setFirstNode(0L);
    setProgNode(0L);

    clearException();
    retVal = 0L;

    delete con; con = 0L;
    glob.clear();

    Collector::collect();

    // remove from global chain (see init())
    next->prev = prev;
    prev->next = next;
    hook = next;
    if (hook == this)
      hook = 0L;

#ifdef KJS_DEBUGGER
    sid = -1;
#endif

    initialized = false;
  }
  if (old != this)
      KJScriptImp::curr = old;
}

bool KJScriptImp::evaluate(const UChar *code, unsigned int length, const KJSO &thisV,
			   bool onlyCheckSyntax)
{
  init();

#ifdef KJS_DEBUGGER
  sid++;
  if (debugger())
    debugger()->setSourceId(sid);
#endif
  if (recursion > 7) {
    fprintf(stderr, "KJS: breaking out of recursion\n");
    return true;
  } else if (recursion > 0) {
#ifndef NDEBUG
    fprintf(stderr, "KJS: entering recursion level %d\n", recursion);
#endif
    pushStack();
  }

  assert(Lexer::curr());
  Lexer::curr()->setCode(code, length);
  Node::setFirstNode(firstNode());
  int parseError = kjsyyparse();
  setFirstNode(Node::firstNode());

  if (parseError) {
    errType = 99; /* TODO */
    errLine = Lexer::curr()->lineNo();
    errMsg = "Parse error at line " + UString::from(errLine);
#ifndef NDEBUG
    fprintf(stderr, "JavaScript parse error at line %d.\n", errLine);
#endif
    /* TODO: either clear everything or keep previously
       parsed function definitions */
    //    Node::deleteAllNodes();
    return false;
  }

  if (onlyCheckSyntax)
      return true;

  clearException();

  KJSO oldVar;
  if (!thisV.isNull()) {
    context()->setThisValue(thisV);
    context()->pushScope(thisV);
    oldVar = context()->variableObject();
    context()->setVariableObject(thisV);
  }

  running++;
  recursion++;
  assert(progNode());
  Completion res = progNode()->execute();
  recursion--;
  running--;

  if (hadException()) {
    KJSO err = exception();
    errType = 99; /* TODO */
    errLine = err.get("line").toInt32();
    errMsg = err.get("name").toString().value() + ". ";
    errMsg += err.get("message").toString().value();
#ifdef KJS_DEBUGGER
    if (dbg)
      dbg->setSourceId(err.get("sid").toInt32());
#endif
    clearException();
  } else {
    errType = 0;
    errLine = -1;
    errMsg = "";

    // catch return value
    retVal = 0L;
    if (res.complType() == ReturnValue || !thisV.isNull())
	retVal = res.value().imp();
  }

  if (!thisV.isNull()) {
    context()->popScope();
    context()->setVariableObject(oldVar);
  }

  if (progNode())
    progNode()->deleteGlobalStatements();

  if (recursion > 0) {
    popStack();
  }

  return !errType;
}

void KJScriptImp::pushStack()
{
    stack = stack->push();
}

void KJScriptImp::popStack()
{
    stack = stack->pop();
    assert(stack);
}

bool KJScriptImp::call(const KJSO &scope, const UString &func, const List &args)
{
  init();
  KJSO callScope(scope);
  if (callScope.isNull())
    callScope = Global::current().imp();
  if (!callScope.hasProperty(func)) {
#ifndef NDEBUG
      fprintf(stderr, "couldn't resolve function name %s. call() failed\n",
	      func.ascii());
#endif
      return false;
  }
  KJSO v = callScope.get(func);
  if (!v.isA(ConstructorType)) {
#ifndef NDEBUG
      fprintf(stderr, "%s is not a function. call() failed.\n", func.ascii());
#endif
      return false;
  }
  running++;
  recursion++;
  static_cast<ConstructorImp*>(v.imp())->executeCall(scope.imp(), &args);
  recursion--;
  running--;
  return !hadException();
}

bool KJScriptImp::call(const KJSO &func, const KJSO &thisV,
		       const List &args, const List &extraScope)
{
  init();
  if(!func.implementsCall())
    return false;

  running++;
  recursion++;
  retVal = func.executeCall(thisV, &args, &extraScope).imp();
  recursion--;
  running--;

  return !hadException();
}

void KJScriptImp::setException(Imp *e)
{
  assert(curr);
  curr->exVal = e;
  curr->exMsg = "Exception"; // not very meaningful but we use !0L to test
}

void KJScriptImp::setException(const char *msg)
{
  assert(curr);
  curr->exVal = 0L;		// will be created later on exception()
  curr->exMsg = msg;
}

KJSO KJScriptImp::exception()
{
  assert(curr);
  if (!curr->exMsg)
    return Undefined();
  if (curr->exVal)
    return curr->exVal;
  return Error::create(GeneralError, curr->exMsg);
}

void KJScriptImp::clearException()
{
  assert(curr);
  curr->exMsg = 0L;
  curr->exVal = 0L;
}

#ifdef KJS_DEBUGGER
void KJScriptImp::attachDebugger(Debugger *d)
{
  static bool detaching = false;
  if (detaching) // break circular detaching
    return;

  if (dbg) {
    detaching = true;
    dbg->detach();
    detaching = false;
  }

  dbg = d;
}

bool KJScriptImp::setBreakpoint(int id, int line, bool set)
{
  init();
  return Node::setBreakpoint(firstNode(), id, line, set);
}

#endif

bool PropList::contains(const UString &name)
{
  PropList *p = this;
  while (p) {
    if (name == p->name)
      return true;
    p = p->next;
  }
  return false;
}

bool LabelStack::push(const UString &id)
{
  if (id.isEmpty() || contains(id))
    return false;

  StackElm *newtos = new StackElm;
  newtos->id = id;
  newtos->prev = tos;
  tos = newtos;
  return true;
}

bool LabelStack::contains(const UString &id) const
{
  if (id.isEmpty())
    return true;

  for (StackElm *curr = tos; curr; curr = curr->prev)
    if (curr->id == id)
      return true;

  return false;
}

void LabelStack::pop()
{
  if (tos) {
    StackElm *prev = tos->prev;
    delete tos;
    tos = prev;
  }
}

LabelStack::~LabelStack()
{
  StackElm *prev;

  while (tos) {
    prev = tos->prev;
    delete tos;
    tos = prev;
  }
}

// ECMA 15.3.5.3 [[HasInstance]]
// see comment in header file
KJSO KJS::hasInstance(const KJSO &F, const KJSO &V)
{
  if (V.isObject()) {
    KJSO prot = F.get("prototype");
    if (!prot.isObject())
      return Error::create(TypeError, "Invalid prototype encountered "
			   "in instanceof operation.");
    Imp *v = V.imp();
    while ((v = v->prototype())) {
      if (v == prot.imp())
	return Boolean(true);
    }
  }
  return Boolean(false);
}

#ifndef NDEBUG
#include <stdio.h>
void KJS::printInfo( const char *s, const KJSO &o )
{
    if (o.isNull())
      fprintf(stderr, "%s: (null)\n", s);
    else {
      KJSO v = o;
      if (o.isA(ReferenceType))
	  v = o.getValue();
      fprintf(stderr, "JS: %s: %s : %s (%p)\n",
	      s,
	      v.toString().value().ascii(),
	      v.imp()->typeInfo()->name,
	      (void*)v.imp());
      if (o.isA(ReferenceType)) {
	  fprintf(stderr, "JS: Was property '%s'\n", o.getPropertyName().ascii());
	  printInfo("of", o.getBase());
      }
    }
}
#endif