source: webkit/trunk/JavaScriptCore/kjs/nodes2string.cpp@ 10218

Last change on this file since 10218 was 10218, checked in by darin, 20 years ago

Reviewed by Geoff.

  • clean up exported symbols that are not in a "KJS" namespace
  • bindings/NP_jsobject.cpp: (identiferFromNPIdentifier): Marked this function static so it no longer has external linkage.
  • bindings/c/c_utility.h: Put all this stuff inside the KJS namespace.
  • bindings/c/c_utility.cpp: Also marked some globals static so they don't have external linkage; not as important given the namespace.
  • bindings/npruntime.cpp: Marked functions static so they no longer have internal linkage. Also removed unused _NPN_SetExceptionWithUTF8 function (not in header, had C++ linkage!).
  • bindings/jni/jni_utility.cpp: (KJS::Bindings::getJavaVM): Call KJS_GetCreatedJavaVMs using the soft linking header, instead of calling the JNI call. This allows processes to link both JavaScriptCore and JavaVM without a symbol conflict.
  • bindings/softlinking.c: (loadFramework): Marked this function static so it no longer has external linkage. (getFunctionPointer): Ditto. (KJS_GetCreatedJavaVMs): Renamed this so it has a KJS prefix.
  • JavaScriptCore.xcodeproj/project.pbxproj: Added softlinking.h.
  • bindings/softlinking.h: Added.
  • kjs/nodes2string.cpp: (streamAssignmentOperatorTo): Marked this function static so it no longer has external linkage.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 2002 Harri Porten ([email protected])
5 * Copyright (C) 2003 Apple Computer, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#include "nodes.h"
25
26namespace KJS {
27 /**
28 * A simple text streaming class that helps with code indentation.
29 */
30 class SourceStream {
31 public:
32 enum Format {
33 Endl, Indent, Unindent
34 };
35
36 UString toString() const { return str; }
37 SourceStream& operator<<(const Identifier &);
38 SourceStream& operator<<(const UString &);
39 SourceStream& operator<<(const char *);
40 SourceStream& operator<<(char);
41 SourceStream& operator<<(Format f);
42 SourceStream& operator<<(const Node *);
43 private:
44 UString str; /* TODO: buffer */
45 UString ind;
46 };
47};
48
49using namespace KJS;
50
51SourceStream& SourceStream::operator<<(char c)
52{
53 str += UString(c);
54 return *this;
55}
56
57SourceStream& SourceStream::operator<<(const char *s)
58{
59 str += UString(s);
60 return *this;
61}
62
63SourceStream& SourceStream::operator<<(const UString &s)
64{
65 str += s;
66 return *this;
67}
68
69SourceStream& SourceStream::operator<<(const Identifier &s)
70{
71 str += s.ustring();
72 return *this;
73}
74
75SourceStream& SourceStream::operator<<(const Node *n)
76{
77 if (n)
78 n->streamTo(*this);
79 return *this;
80}
81
82SourceStream& SourceStream::operator<<(Format f)
83{
84 switch (f) {
85 case Endl:
86 str += "\n" + ind;
87 break;
88 case Indent:
89 ind += " ";
90 break;
91 case Unindent:
92 ind = ind.substr(0, ind.size() - 2);
93 break;
94 }
95
96 return *this;
97}
98
99UString Node::toString() const
100{
101 SourceStream str;
102 streamTo(str);
103
104 return str.toString();
105}
106
107void NullNode::streamTo(SourceStream &s) const { s << "null"; }
108
109void BooleanNode::streamTo(SourceStream &s) const
110{
111 s << (value ? "true" : "false");
112}
113
114void NumberNode::streamTo(SourceStream &s) const { s << UString::from(value); }
115
116void StringNode::streamTo(SourceStream &s) const
117{
118 s << '"' << value << '"';
119}
120
121void RegExpNode::streamTo(SourceStream &s) const { s << pattern; }
122
123void ThisNode::streamTo(SourceStream &s) const { s << "this"; }
124
125void ResolveNode::streamTo(SourceStream &s) const { s << ident; }
126
127void GroupNode::streamTo(SourceStream &s) const
128{
129 s << "(" << group << ")";
130}
131
132void ElementNode::streamTo(SourceStream &s) const
133{
134 for (const ElementNode *n = this; n; n = n->list) {
135 for (int i = 0; i < n->elision; i++)
136 s << ",";
137 s << n->node;
138 }
139}
140
141void ArrayNode::streamTo(SourceStream &s) const
142{
143 s << "[" << element;
144 for (int i = 0; i < elision; i++)
145 s << ",";
146 s << "]";
147}
148
149void ObjectLiteralNode::streamTo(SourceStream &s) const
150{
151 if (list)
152 s << "{ " << list << " }";
153 else
154 s << "{ }";
155}
156
157void PropertyValueNode::streamTo(SourceStream &s) const
158{
159 for (const PropertyValueNode *n = this; n; n = n->list)
160 s << n->name << ": " << n->assign;
161}
162
163void PropertyNode::streamTo(SourceStream &s) const
164{
165 if (str.isNull())
166 s << UString::from(numeric);
167 else
168 s << str;
169}
170
171void BracketAccessorNode::streamTo(SourceStream &s) const
172{
173 s << expr1 << "[" << expr2 << "]";
174}
175
176void DotAccessorNode::streamTo(SourceStream &s) const
177{
178 s << expr << "." << ident;
179}
180
181void ArgumentListNode::streamTo(SourceStream &s) const
182{
183 s << expr;
184 for (ArgumentListNode *n = list; n; n = n->list)
185 s << ", " << n->expr;
186}
187
188void ArgumentsNode::streamTo(SourceStream &s) const
189{
190 s << "(" << list << ")";
191}
192
193void NewExprNode::streamTo(SourceStream &s) const
194{
195 s << "new " << expr << args;
196}
197
198void FunctionCallValueNode::streamTo(SourceStream &s) const
199{
200 s << expr << args;
201}
202
203void FunctionCallResolveNode::streamTo(SourceStream &s) const
204{
205 s << ident << args;
206}
207
208void FunctionCallBracketNode::streamTo(SourceStream &s) const
209{
210 s << base << "[" << subscript << "]" << args;
211}
212
213void FunctionCallParenBracketNode::streamTo(SourceStream &s) const
214{
215 s << "(" << base << "[" << subscript << "])" << args;
216}
217
218void FunctionCallDotNode::streamTo(SourceStream &s) const
219{
220 s << base << "." << ident << args;
221}
222
223void FunctionCallParenDotNode::streamTo(SourceStream &s) const
224{
225 s << "(" << base << "." << ident << ")" << args;
226}
227
228void PostfixNode::streamTo(SourceStream &s) const
229{
230 s << expr;
231 if (oper == OpPlusPlus)
232 s << "++";
233 else
234 s << "--";
235}
236
237void DeleteNode::streamTo(SourceStream &s) const
238{
239 s << "delete " << expr;
240}
241
242void VoidNode::streamTo(SourceStream &s) const
243{
244 s << "void " << expr;
245}
246
247void TypeOfNode::streamTo(SourceStream &s) const
248{
249 s << "typeof " << expr;
250}
251
252void PrefixNode::streamTo(SourceStream &s) const
253{
254 s << expr << (oper == OpPlusPlus ? "++" : "--");
255}
256
257void UnaryPlusNode::streamTo(SourceStream &s) const
258{
259 s << "+" << expr;
260}
261
262void NegateNode::streamTo(SourceStream &s) const
263{
264 s << "-" << expr;
265}
266
267void BitwiseNotNode::streamTo(SourceStream &s) const
268{
269 s << "~" << expr;
270}
271
272void LogicalNotNode::streamTo(SourceStream &s) const
273{
274 s << "!" << expr;
275}
276
277void MultNode::streamTo(SourceStream &s) const
278{
279 s << term1 << oper << term2;
280}
281
282void AddNode::streamTo(SourceStream &s) const
283{
284 s << term1 << oper << term2;
285}
286
287void ShiftNode::streamTo(SourceStream &s) const
288{
289 s << term1;
290 if (oper == OpLShift)
291 s << "<<";
292 else if (oper == OpRShift)
293 s << ">>";
294 else
295 s << ">>>";
296 s << term2;
297}
298
299void RelationalNode::streamTo(SourceStream &s) const
300{
301 s << expr1;
302 switch (oper) {
303 case OpLess:
304 s << " < ";
305 break;
306 case OpGreater:
307 s << " > ";
308 break;
309 case OpLessEq:
310 s << " <= ";
311 break;
312 case OpGreaterEq:
313 s << " >= ";
314 break;
315 case OpInstanceOf:
316 s << " instanceof ";
317 break;
318 case OpIn:
319 s << " in ";
320 break;
321 default:
322 ;
323 }
324 s << expr2;
325}
326
327void EqualNode::streamTo(SourceStream &s) const
328{
329 s << expr1;
330 switch (oper) {
331 case OpEqEq:
332 s << " == ";
333 break;
334 case OpNotEq:
335 s << " != ";
336 break;
337 case OpStrEq:
338 s << " === ";
339 break;
340 case OpStrNEq:
341 s << " !== ";
342 break;
343 default:
344 ;
345 }
346 s << expr2;
347}
348
349void BitOperNode::streamTo(SourceStream &s) const
350{
351 s << expr1;
352 if (oper == OpBitAnd)
353 s << " & ";
354 else if (oper == OpBitXOr)
355 s << " ^ ";
356 else
357 s << " | ";
358 s << expr2;
359}
360
361void BinaryLogicalNode::streamTo(SourceStream &s) const
362{
363 s << expr1 << (oper == OpAnd ? " && " : " || ") << expr2;
364}
365
366void ConditionalNode::streamTo(SourceStream &s) const
367{
368 s << logical << " ? " << expr1 << " : " << expr2;
369}
370
371static void streamAssignmentOperatorTo(SourceStream &s, Operator oper)
372{
373 const char *opStr;
374 switch (oper) {
375 case OpEqual:
376 opStr = " = ";
377 break;
378 case OpMultEq:
379 opStr = " *= ";
380 break;
381 case OpDivEq:
382 opStr = " /= ";
383 break;
384 case OpPlusEq:
385 opStr = " += ";
386 break;
387 case OpMinusEq:
388 opStr = " -= ";
389 break;
390 case OpLShift:
391 opStr = " <<= ";
392 break;
393 case OpRShift:
394 opStr = " >>= ";
395 break;
396 case OpURShift:
397 opStr = " >>>= ";
398 break;
399 case OpAndEq:
400 opStr = " &= ";
401 break;
402 case OpXOrEq:
403 opStr = " ^= ";
404 break;
405 case OpOrEq:
406 opStr = " |= ";
407 break;
408 case OpModEq:
409 opStr = " %= ";
410 break;
411 default:
412 opStr = " ?= ";
413 }
414 s << opStr;
415}
416
417void AssignResolveNode::streamTo(SourceStream &s) const
418{
419 s << m_ident;
420 streamAssignmentOperatorTo(s, m_oper);
421 s << m_right;
422}
423
424void AssignBracketNode::streamTo(SourceStream &s) const
425{
426 s << m_base << "[" << m_subscript << "]";
427 streamAssignmentOperatorTo(s, m_oper);
428 s << m_right;
429}
430
431void AssignDotNode::streamTo(SourceStream &s) const
432{
433 s << m_base << "." << m_ident;
434 streamAssignmentOperatorTo(s, m_oper);
435 s << m_right;
436}
437
438void CommaNode::streamTo(SourceStream &s) const
439{
440 s << expr1 << ", " << expr2;
441}
442
443void StatListNode::streamTo(SourceStream &s) const
444{
445 for (const StatListNode *n = this; n; n = n->list)
446 s << n->statement;
447}
448
449void AssignExprNode::streamTo(SourceStream &s) const
450{
451 s << " = " << expr;
452}
453
454void VarDeclNode::streamTo(SourceStream &s) const
455{
456 s << ident << init;
457}
458
459void VarDeclListNode::streamTo(SourceStream &s) const
460{
461 s << var;
462 for (VarDeclListNode *n = list; n; n = n->list)
463 s << ", " << n->var;
464}
465
466void VarStatementNode::streamTo(SourceStream &s) const
467{
468 s << SourceStream::Endl << "var " << list << ";";
469}
470
471void BlockNode::streamTo(SourceStream &s) const
472{
473 s << SourceStream::Endl << "{" << SourceStream::Indent
474 << source << SourceStream::Unindent << SourceStream::Endl << "}";
475}
476
477void EmptyStatementNode::streamTo(SourceStream &s) const
478{
479 s << SourceStream::Endl << ";";
480}
481
482void ExprStatementNode::streamTo(SourceStream &s) const
483{
484 s << SourceStream::Endl << expr << ";";
485}
486
487void IfNode::streamTo(SourceStream &s) const
488{
489 s << SourceStream::Endl << "if (" << expr << ")" << SourceStream::Indent
490 << statement1 << SourceStream::Unindent;
491 if (statement2)
492 s << SourceStream::Endl << "else" << SourceStream::Indent
493 << statement2 << SourceStream::Unindent;
494}
495
496void DoWhileNode::streamTo(SourceStream &s) const
497{
498 s << SourceStream::Endl << "do " << SourceStream::Indent
499 << statement << SourceStream::Unindent << SourceStream::Endl
500 << "while (" << expr << ");";
501}
502
503void WhileNode::streamTo(SourceStream &s) const
504{
505 s << SourceStream::Endl << "while (" << expr << ")" << SourceStream::Indent
506 << statement << SourceStream::Unindent;
507}
508
509void ForNode::streamTo(SourceStream &s) const
510{
511 s << SourceStream::Endl << "for ("
512 << expr1 // TODO: doesn't properly do "var i = 0"
513 << "; " << expr2
514 << "; " << expr3
515 << ")" << SourceStream::Indent << statement << SourceStream::Unindent;
516}
517
518void ForInNode::streamTo(SourceStream &s) const
519{
520 s << SourceStream::Endl << "for (";
521 if (varDecl)
522 s << "var " << varDecl;
523 if (init)
524 s << " = " << init;
525 s << " in " << expr << ")" << SourceStream::Indent
526 << statement << SourceStream::Unindent;
527}
528
529void ContinueNode::streamTo(SourceStream &s) const
530{
531 s << SourceStream::Endl << "continue";
532 if (!ident.isNull())
533 s << " " << ident;
534 s << ";";
535}
536
537void BreakNode::streamTo(SourceStream &s) const
538{
539 s << SourceStream::Endl << "break";
540 if (!ident.isNull())
541 s << " " << ident;
542 s << ";";
543}
544
545void ReturnNode::streamTo(SourceStream &s) const
546{
547 s << SourceStream::Endl << "return";
548 if (value)
549 s << " " << value;
550 s << ";";
551}
552
553void WithNode::streamTo(SourceStream &s) const
554{
555 s << SourceStream::Endl << "with (" << expr << ") "
556 << statement;
557}
558
559void CaseClauseNode::streamTo(SourceStream &s) const
560{
561 s << SourceStream::Endl;
562 if (expr)
563 s << "case " << expr;
564 else
565 s << "default";
566 s << ":" << SourceStream::Indent;
567 if (list)
568 s << list;
569 s << SourceStream::Unindent;
570}
571
572void ClauseListNode::streamTo(SourceStream &s) const
573{
574 for (const ClauseListNode *n = this; n; n = n->next())
575 s << n->clause();
576}
577
578void CaseBlockNode::streamTo(SourceStream &s) const
579{
580 for (const ClauseListNode *n = list1; n; n = n->next())
581 s << n->clause();
582 if (def)
583 s << def;
584 for (const ClauseListNode *n = list2; n; n = n->next())
585 s << n->clause();
586}
587
588void SwitchNode::streamTo(SourceStream &s) const
589{
590 s << SourceStream::Endl << "switch (" << expr << ") {"
591 << SourceStream::Indent << block << SourceStream::Unindent
592 << SourceStream::Endl << "}";
593}
594
595void LabelNode::streamTo(SourceStream &s) const
596{
597 s << SourceStream::Endl << label << ":" << SourceStream::Indent
598 << statement << SourceStream::Unindent;
599}
600
601void ThrowNode::streamTo(SourceStream &s) const
602{
603 s << SourceStream::Endl << "throw " << expr << ";";
604}
605
606void CatchNode::streamTo(SourceStream &s) const
607{
608 s << SourceStream::Endl << "catch (" << ident << ")" << block;
609}
610
611void FinallyNode::streamTo(SourceStream &s) const
612{
613 s << SourceStream::Endl << "finally " << block;
614}
615
616void TryNode::streamTo(SourceStream &s) const
617{
618 s << "try " << block
619 << _catch
620 << _final;
621}
622
623void ParameterNode::streamTo(SourceStream &s) const
624{
625 s << id;
626 for (ParameterNode *n = next; n; n = n->next)
627 s << ", " << n->id;
628}
629
630void FuncDeclNode::streamTo(SourceStream &s) const {
631 s << "function " << ident << "(";
632 if (param)
633 s << param;
634 s << ")" << body;
635}
636
637void FuncExprNode::streamTo(SourceStream &s) const
638{
639 s << "function " << "("
640 << param
641 << ")" << body;
642}
643
644void SourceElementsNode::streamTo(SourceStream &s) const
645{
646 for (const SourceElementsNode *n = this; n; n = n->elements)
647 s << n->element;
648}
649
Note: See TracBrowser for help on using the repository browser.