forked from TritonDataCenter/javascriptlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsscript.c
More file actions
1717 lines (1499 loc) · 50.1 KB
/
jsscript.c
File metadata and controls
1717 lines (1499 loc) · 50.1 KB
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sw=4 et tw=78:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* JS script operations.
*/
#include "jsstddef.h"
#include <string.h>
#include "jstypes.h"
#include "jsutil.h" /* Added by JSIFY */
#include "jsprf.h"
#include "jsapi.h"
#include "jsatom.h"
#include "jscntxt.h"
#include "jsconfig.h"
#include "jsdbgapi.h"
#include "jsemit.h"
#include "jsfun.h"
#include "jsinterp.h"
#include "jslock.h"
#include "jsnum.h"
#include "jsopcode.h"
#include "jsscript.h"
#if JS_HAS_XDR
#include "jsxdrapi.h"
#endif
#if JS_HAS_SCRIPT_OBJECT
static const char js_script_exec[] = "Script.prototype.exec";
static const char js_script_compile[] = "Script.prototype.compile";
/*
* This routine requires that obj has been locked previously.
*/
static jsint
GetScriptExecDepth(JSContext *cx, JSObject *obj)
{
jsval v;
JS_ASSERT(JS_IS_OBJ_LOCKED(cx, obj));
v = LOCKED_OBJ_GET_SLOT(obj, JSSLOT_START(&js_ScriptClass));
return JSVAL_TO_INT(v);
}
static void
AdjustScriptExecDepth(JSContext *cx, JSObject *obj, jsint delta)
{
jsint execDepth;
JS_LOCK_OBJ(cx, obj);
execDepth = GetScriptExecDepth(cx, obj);
LOCKED_OBJ_SET_SLOT(obj, JSSLOT_START(&js_ScriptClass),
INT_TO_JSVAL(execDepth + delta));
JS_UNLOCK_OBJ(cx, obj);
}
#if JS_HAS_TOSOURCE
static JSBool
script_toSource(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
uint32 indent;
JSScript *script;
size_t i, j, k, n;
char buf[16];
jschar *s, *t;
JSString *str;
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, argv))
return JS_FALSE;
indent = 0;
if (argc && !js_ValueToECMAUint32(cx, argv[0], &indent))
return JS_FALSE;
script = (JSScript *) JS_GetPrivate(cx, obj);
/* Let n count the source string length, j the "front porch" length. */
j = JS_snprintf(buf, sizeof buf, "(new %s(", js_ScriptClass.name);
n = j + 2;
if (!script) {
/* Let k count the constructor argument string length. */
k = 0;
s = NULL; /* quell GCC overwarning */
} else {
str = JS_DecompileScript(cx, script, "Script.prototype.toSource",
(uintN)indent);
if (!str)
return JS_FALSE;
str = js_QuoteString(cx, str, '\'');
if (!str)
return JS_FALSE;
s = JSSTRING_CHARS(str);
k = JSSTRING_LENGTH(str);
n += k;
}
/* Allocate the source string and copy into it. */
t = (jschar *) JS_malloc(cx, (n + 1) * sizeof(jschar));
if (!t)
return JS_FALSE;
for (i = 0; i < j; i++)
t[i] = buf[i];
for (j = 0; j < k; i++, j++)
t[i] = s[j];
t[i++] = ')';
t[i++] = ')';
t[i] = 0;
/* Create and return a JS string for t. */
str = JS_NewUCString(cx, t, n);
if (!str) {
JS_free(cx, t);
return JS_FALSE;
}
*rval = STRING_TO_JSVAL(str);
return JS_TRUE;
}
#endif /* JS_HAS_TOSOURCE */
static JSBool
script_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
uint32 indent;
JSScript *script;
JSString *str;
indent = 0;
if (argc && !js_ValueToECMAUint32(cx, argv[0], &indent))
return JS_FALSE;
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, argv))
return JS_FALSE;
script = (JSScript *) JS_GetPrivate(cx, obj);
if (!script) {
*rval = STRING_TO_JSVAL(cx->runtime->emptyString);
return JS_TRUE;
}
str = JS_DecompileScript(cx, script, "Script.prototype.toString",
(uintN)indent);
if (!str)
return JS_FALSE;
*rval = STRING_TO_JSVAL(str);
return JS_TRUE;
}
static JSBool
script_compile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
JSString *str;
JSObject *scopeobj;
jsval v;
JSScript *script, *oldscript;
JSStackFrame *fp, *caller;
const char *file;
uintN line;
JSPrincipals *principals;
jsint execDepth;
/* Make sure obj is a Script object. */
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, argv))
return JS_FALSE;
/* If no args, leave private undefined and return early. */
if (argc == 0)
goto out;
/* Otherwise, the first arg is the script source to compile. */
str = js_ValueToString(cx, argv[0]);
if (!str)
return JS_FALSE;
argv[0] = STRING_TO_JSVAL(str);
scopeobj = NULL;
if (argc >= 2) {
if (!js_ValueToObject(cx, argv[1], &scopeobj))
return JS_FALSE;
argv[1] = OBJECT_TO_JSVAL(scopeobj);
}
/* Compile using the caller's scope chain, which js_Invoke passes to fp. */
fp = cx->fp;
caller = JS_GetScriptedCaller(cx, fp);
JS_ASSERT(!caller || fp->scopeChain == caller->scopeChain);
if (caller) {
if (!scopeobj) {
scopeobj = js_GetScopeChain(cx, caller);
if (!scopeobj)
return JS_FALSE;
fp->scopeChain = scopeobj; /* for the compiler's benefit */
}
principals = JS_EvalFramePrincipals(cx, fp, caller);
if (principals == caller->script->principals) {
file = caller->script->filename;
line = js_PCToLineNumber(cx, caller->script, caller->pc);
} else {
file = principals->codebase;
line = 0;
}
} else {
file = NULL;
line = 0;
principals = NULL;
}
/* Ensure we compile this script with the right (inner) principals. */
scopeobj = js_CheckScopeChainValidity(cx, scopeobj, js_script_compile);
if (!scopeobj)
return JS_FALSE;
/*
* Compile the new script using the caller's scope chain, a la eval().
* Unlike jsobj.c:obj_eval, however, we do not set JSFRAME_EVAL in fp's
* flags, because compilation is here separated from execution, and the
* run-time scope chain may not match the compile-time. JSFRAME_EVAL is
* tested in jsemit.c and jsscan.c to optimize based on identity of run-
* and compile-time scope.
*/
fp->flags |= JSFRAME_SCRIPT_OBJECT;
script = JS_CompileUCScriptForPrincipals(cx, scopeobj, principals,
JSSTRING_CHARS(str),
JSSTRING_LENGTH(str),
file, line);
if (!script)
return JS_FALSE;
JS_LOCK_OBJ(cx, obj);
execDepth = GetScriptExecDepth(cx, obj);
/*
* execDepth must be 0 to allow compilation here, otherwise the JSScript
* struct can be released while running.
*/
if (execDepth > 0) {
JS_UNLOCK_OBJ(cx, obj);
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_COMPILE_EXECED_SCRIPT);
return JS_FALSE;
}
/* Swap script for obj's old script, if any. */
v = LOCKED_OBJ_GET_SLOT(obj, JSSLOT_PRIVATE);
oldscript = !JSVAL_IS_VOID(v) ? (JSScript *) JSVAL_TO_PRIVATE(v) : NULL;
LOCKED_OBJ_SET_SLOT(obj, JSSLOT_PRIVATE, PRIVATE_TO_JSVAL(script));
JS_UNLOCK_OBJ(cx, obj);
if (oldscript)
js_DestroyScript(cx, oldscript);
script->object = obj;
js_CallNewScriptHook(cx, script, NULL);
out:
/* Return the object. */
*rval = OBJECT_TO_JSVAL(obj);
return JS_TRUE;
}
static JSBool
script_exec(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
JSObject *scopeobj, *parent;
JSStackFrame *fp, *caller;
JSScript *script;
JSBool ok;
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, argv))
return JS_FALSE;
scopeobj = NULL;
if (argc) {
if (!js_ValueToObject(cx, argv[0], &scopeobj))
return JS_FALSE;
argv[0] = OBJECT_TO_JSVAL(scopeobj);
}
/*
* Emulate eval() by using caller's this, var object, sharp array, etc.,
* all propagated by js_Execute via a non-null fourth (down) argument to
* js_Execute. If there is no scripted caller, js_Execute uses its second
* (chain) argument to set the exec frame's varobj, thisp, and scopeChain.
*
* Unlike eval, which the compiler detects, Script.prototype.exec may be
* called from a lightweight function, or even from native code (in which
* case fp->varobj and fp->scopeChain are null). If exec is called from
* a lightweight function, we will need to get a Call object representing
* its frame, to act as the var object and scope chain head.
*/
fp = cx->fp;
caller = JS_GetScriptedCaller(cx, fp);
if (caller && !caller->varobj) {
/* Called from a lightweight function. */
JS_ASSERT(caller->fun && !JSFUN_HEAVYWEIGHT_TEST(caller->fun->flags));
/* Scope chain links from Call object to callee's parent. */
parent = OBJ_GET_PARENT(cx, JSVAL_TO_OBJECT(caller->argv[-2]));
if (!js_GetCallObject(cx, caller, parent))
return JS_FALSE;
}
if (!scopeobj) {
/* No scope object passed in: try to use the caller's scope chain. */
if (caller) {
/*
* Load caller->scopeChain after the conditional js_GetCallObject
* call above, which resets scopeChain as well as varobj.
*/
scopeobj = js_GetScopeChain(cx, caller);
if (!scopeobj)
return JS_FALSE;
} else {
/*
* Called from native code, so we don't know what scope object to
* use. We could use parent (see above), but Script.prototype.exec
* might be a shared/sealed "superglobal" method. A more general
* approach would use cx->globalObject, which will be the same as
* exec.__parent__ in the non-superglobal case. In the superglobal
* case it's the right object: the global, not the superglobal.
*/
scopeobj = cx->globalObject;
}
}
scopeobj = js_CheckScopeChainValidity(cx, scopeobj, js_script_exec);
if (!scopeobj)
return JS_FALSE;
/* Keep track of nesting depth for the script. */
AdjustScriptExecDepth(cx, obj, 1);
/* Must get to out label after this */
script = (JSScript *) JS_GetPrivate(cx, obj);
if (!script) {
ok = JS_FALSE;
goto out;
}
/* Belt-and-braces: check that this script object has access to scopeobj. */
ok = js_CheckPrincipalsAccess(cx, scopeobj, script->principals,
CLASS_ATOM(cx, Script));
if (!ok)
goto out;
ok = js_Execute(cx, scopeobj, script, caller, JSFRAME_EVAL, rval);
out:
AdjustScriptExecDepth(cx, obj, -1);
return ok;
}
#if JS_HAS_XDR
static JSBool
XDRAtomMap(JSXDRState *xdr, JSAtomMap *map)
{
JSContext *cx;
uint32 natoms, i, index;
JSAtom **atoms;
cx = xdr->cx;
if (xdr->mode == JSXDR_ENCODE)
natoms = (uint32)map->length;
if (!JS_XDRUint32(xdr, &natoms))
return JS_FALSE;
if (xdr->mode == JSXDR_ENCODE) {
atoms = map->vector;
} else {
if (natoms == 0) {
atoms = NULL;
} else {
atoms = (JSAtom **) JS_malloc(cx, (size_t)natoms * sizeof *atoms);
if (!atoms)
return JS_FALSE;
#ifdef DEBUG
memset(atoms, 0, (size_t)natoms * sizeof *atoms);
#endif
}
map->vector = atoms;
map->length = natoms;
}
for (i = 0; i != natoms; ++i) {
if (xdr->mode == JSXDR_ENCODE)
index = i;
if (!JS_XDRUint32(xdr, &index))
goto bad;
/*
* Assert that, when decoding, the read index is valid and points to
* an unoccupied element of atoms array.
*/
JS_ASSERT(index < natoms);
JS_ASSERT(xdr->mode == JSXDR_ENCODE || !atoms[index]);
if (!js_XDRAtom(xdr, &atoms[index]))
goto bad;
}
return JS_TRUE;
bad:
if (xdr->mode == JSXDR_DECODE) {
JS_free(cx, atoms);
map->vector = NULL;
map->length = 0;
}
return JS_FALSE;
}
JSBool
js_XDRScript(JSXDRState *xdr, JSScript **scriptp, JSBool *hasMagic)
{
JSContext *cx;
JSScript *script, *newscript, *oldscript;
uint32 length, lineno, depth, magic, nsrcnotes, ntrynotes;
uint32 prologLength, version;
JSBool filenameWasSaved;
jssrcnote *notes, *sn;
cx = xdr->cx;
script = *scriptp;
nsrcnotes = ntrynotes = 0;
filenameWasSaved = JS_FALSE;
notes = NULL;
/*
* Encode prologLength and version after script->length (_2 or greater),
* but decode both new (>= _2) and old, prolog&version-free (_1) scripts.
* Version _3 supports principals serialization. Version _4 reorders the
* nsrcnotes and ntrynotes fields to come before everything except magic,
* length, prologLength, and version, so that srcnote and trynote storage
* can be allocated as part of the JSScript (along with bytecode storage).
*
* So far, the magic number has not changed for every jsopcode.tbl change.
* We stipulate forward compatibility by requiring old bytecodes never to
* change or go away (modulo a few exceptions before the XDR interfaces
* evolved, and a few exceptions during active trunk development). With
* the addition of JSOP_STOP to support JS_THREADED_INTERP, we make a new
* magic number (_5) so that we know to append JSOP_STOP to old scripts
* when deserializing.
*/
if (xdr->mode == JSXDR_ENCODE)
magic = JSXDR_MAGIC_SCRIPT_CURRENT;
if (!JS_XDRUint32(xdr, &magic))
return JS_FALSE;
JS_ASSERT((uint32)JSXDR_MAGIC_SCRIPT_5 - (uint32)JSXDR_MAGIC_SCRIPT_1 == 4);
if (magic - (uint32)JSXDR_MAGIC_SCRIPT_1 > 4) {
if (!hasMagic) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_BAD_SCRIPT_MAGIC);
return JS_FALSE;
}
*hasMagic = JS_FALSE;
return JS_TRUE;
}
if (hasMagic)
*hasMagic = JS_TRUE;
if (xdr->mode == JSXDR_ENCODE) {
length = script->length;
prologLength = PTRDIFF(script->main, script->code, jsbytecode);
JS_ASSERT((int16)script->version != JSVERSION_UNKNOWN);
version = (uint32)script->version | (script->numGlobalVars << 16);
lineno = (uint32)script->lineno;
depth = (uint32)script->depth;
/* Count the srcnotes, keeping notes pointing at the first one. */
notes = SCRIPT_NOTES(script);
for (sn = notes; !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn))
continue;
nsrcnotes = PTRDIFF(sn, notes, jssrcnote);
nsrcnotes++; /* room for the terminator */
/* Count the trynotes. */
if (script->trynotes) {
while (script->trynotes[ntrynotes].catchStart)
ntrynotes++;
ntrynotes++; /* room for the end marker */
}
}
if (!JS_XDRUint32(xdr, &length))
return JS_FALSE;
if (magic >= JSXDR_MAGIC_SCRIPT_2) {
if (!JS_XDRUint32(xdr, &prologLength))
return JS_FALSE;
if (!JS_XDRUint32(xdr, &version))
return JS_FALSE;
/* To fuse allocations, we need srcnote and trynote counts early. */
if (magic >= JSXDR_MAGIC_SCRIPT_4) {
if (!JS_XDRUint32(xdr, &nsrcnotes))
return JS_FALSE;
if (!JS_XDRUint32(xdr, &ntrynotes))
return JS_FALSE;
}
}
if (xdr->mode == JSXDR_DECODE) {
size_t alloclength = length;
if (magic < JSXDR_MAGIC_SCRIPT_5)
++alloclength; /* add a byte for JSOP_STOP */
script = js_NewScript(cx, alloclength, nsrcnotes, ntrynotes);
if (!script)
return JS_FALSE;
if (magic >= JSXDR_MAGIC_SCRIPT_2) {
script->main += prologLength;
script->version = (JSVersion) (version & 0xffff);
script->numGlobalVars = (uint16) (version >> 16);
/* If we know nsrcnotes, we allocated space for notes in script. */
if (magic >= JSXDR_MAGIC_SCRIPT_4)
notes = SCRIPT_NOTES(script);
}
*scriptp = script;
}
/*
* Control hereafter must goto error on failure, in order for the DECODE
* case to destroy script and conditionally free notes, which if non-null
* in the (DECODE and magic < _4) case must point at a temporary vector
* allocated just below.
*/
oldscript = xdr->script;
xdr->script = script;
if (!JS_XDRBytes(xdr, (char *)script->code, length * sizeof(jsbytecode)) ||
!XDRAtomMap(xdr, &script->atomMap)) {
goto error;
}
if (magic < JSXDR_MAGIC_SCRIPT_5) {
if (xdr->mode == JSXDR_DECODE) {
/*
* Append JSOP_STOP to old scripts, to relieve the interpreter
* from having to bounds-check pc. Also take care to increment
* length, as it is used below and must count all bytecode.
*/
script->code[length++] = JSOP_STOP;
}
if (magic < JSXDR_MAGIC_SCRIPT_4) {
if (!JS_XDRUint32(xdr, &nsrcnotes))
goto error;
if (xdr->mode == JSXDR_DECODE) {
notes = (jssrcnote *)
JS_malloc(cx, nsrcnotes * sizeof(jssrcnote));
if (!notes)
goto error;
}
}
}
if (!JS_XDRBytes(xdr, (char *)notes, nsrcnotes * sizeof(jssrcnote)) ||
!JS_XDRCStringOrNull(xdr, (char **)&script->filename) ||
!JS_XDRUint32(xdr, &lineno) ||
!JS_XDRUint32(xdr, &depth) ||
(magic < JSXDR_MAGIC_SCRIPT_4 && !JS_XDRUint32(xdr, &ntrynotes))) {
goto error;
}
/* Script principals transcoding support comes with versions >= _3. */
if (magic >= JSXDR_MAGIC_SCRIPT_3) {
JSPrincipals *principals;
uint32 encodeable;
if (xdr->mode == JSXDR_ENCODE) {
principals = script->principals;
encodeable = (cx->runtime->principalsTranscoder != NULL);
if (!JS_XDRUint32(xdr, &encodeable))
goto error;
if (encodeable &&
!cx->runtime->principalsTranscoder(xdr, &principals)) {
goto error;
}
} else {
if (!JS_XDRUint32(xdr, &encodeable))
goto error;
if (encodeable) {
if (!cx->runtime->principalsTranscoder) {
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_CANT_DECODE_PRINCIPALS);
goto error;
}
if (!cx->runtime->principalsTranscoder(xdr, &principals))
goto error;
script->principals = principals;
}
}
}
if (xdr->mode == JSXDR_DECODE) {
const char *filename = script->filename;
if (filename) {
filename = js_SaveScriptFilename(cx, filename);
if (!filename)
goto error;
JS_free(cx, (void *) script->filename);
script->filename = filename;
filenameWasSaved = JS_TRUE;
}
script->lineno = (uintN)lineno;
script->depth = (uintN)depth;
if (magic < JSXDR_MAGIC_SCRIPT_4) {
/*
* Argh, we have to reallocate script, copy notes into the extra
* space after the bytecodes, and free the temporary notes vector.
* First, add enough slop to nsrcnotes so we can align the address
* after the srcnotes of the first trynote.
*/
uint32 osrcnotes = nsrcnotes;
if (ntrynotes)
nsrcnotes += JSTRYNOTE_ALIGNMASK;
newscript = (JSScript *) JS_realloc(cx, script,
sizeof(JSScript) +
length * sizeof(jsbytecode) +
nsrcnotes * sizeof(jssrcnote) +
ntrynotes * sizeof(JSTryNote));
if (!newscript)
goto error;
*scriptp = script = newscript;
script->code = (jsbytecode *)(script + 1);
script->main = script->code + prologLength;
memcpy(script->code + length, notes, osrcnotes * sizeof(jssrcnote));
JS_free(cx, (void *) notes);
notes = NULL;
if (ntrynotes) {
script->trynotes = (JSTryNote *)
((jsword)(SCRIPT_NOTES(script) + nsrcnotes) &
~(jsword)JSTRYNOTE_ALIGNMASK);
memset(script->trynotes, 0, ntrynotes * sizeof(JSTryNote));
}
}
}
while (ntrynotes) {
JSTryNote *tn = &script->trynotes[--ntrynotes];
uint32 start = (uint32) tn->start,
catchLength = (uint32) tn->length,
catchStart = (uint32) tn->catchStart;
if (!JS_XDRUint32(xdr, &start) ||
!JS_XDRUint32(xdr, &catchLength) ||
!JS_XDRUint32(xdr, &catchStart)) {
goto error;
}
tn->start = (ptrdiff_t) start;
tn->length = (ptrdiff_t) catchLength;
tn->catchStart = (ptrdiff_t) catchStart;
}
xdr->script = oldscript;
return JS_TRUE;
error:
if (xdr->mode == JSXDR_DECODE) {
if (script->filename && !filenameWasSaved) {
JS_free(cx, (void *) script->filename);
script->filename = NULL;
}
if (notes && magic < JSXDR_MAGIC_SCRIPT_4)
JS_free(cx, (void *) notes);
js_DestroyScript(cx, script);
*scriptp = NULL;
}
return JS_FALSE;
}
#if JS_HAS_XDR_FREEZE_THAW
/*
* These cannot be exposed to web content, and chrome does not need them, so
* we take them out of the Mozilla client altogether. Fortunately, there is
* no way to serialize a native function (see fun_xdrObject in jsfun.c).
*/
static JSBool
script_freeze(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
JSXDRState *xdr;
JSScript *script;
JSBool ok, hasMagic;
uint32 len;
void *buf;
JSString *str;
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, argv))
return JS_FALSE;
script = (JSScript *) JS_GetPrivate(cx, obj);
if (!script)
return JS_TRUE;
/* create new XDR */
xdr = JS_XDRNewMem(cx, JSXDR_ENCODE);
if (!xdr)
return JS_FALSE;
/* write */
ok = js_XDRScript(xdr, &script, &hasMagic);
if (!ok)
goto out;
if (!hasMagic) {
*rval = JSVAL_VOID;
goto out;
}
buf = JS_XDRMemGetData(xdr, &len);
if (!buf) {
ok = JS_FALSE;
goto out;
}
JS_ASSERT((jsword)buf % sizeof(jschar) == 0);
len /= sizeof(jschar);
str = JS_NewUCStringCopyN(cx, (jschar *)buf, len);
if (!str) {
ok = JS_FALSE;
goto out;
}
#if IS_BIG_ENDIAN
{
jschar *chars;
uint32 i;
/* Swap bytes in Unichars to keep frozen strings machine-independent. */
chars = JS_GetStringChars(str);
for (i = 0; i < len; i++)
chars[i] = JSXDR_SWAB16(chars[i]);
}
#endif
*rval = STRING_TO_JSVAL(str);
out:
JS_XDRDestroy(xdr);
return ok;
}
static JSBool
script_thaw(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
JSXDRState *xdr;
JSString *str;
void *buf;
uint32 len;
jsval v;
JSScript *script, *oldscript;
JSBool ok, hasMagic;
if (!JS_InstanceOf(cx, obj, &js_ScriptClass, argv))
return JS_FALSE;
if (argc == 0)
return JS_TRUE;
str = js_ValueToString(cx, argv[0]);
if (!str)
return JS_FALSE;
argv[0] = STRING_TO_JSVAL(str);
/* create new XDR */
xdr = JS_XDRNewMem(cx, JSXDR_DECODE);
if (!xdr)
return JS_FALSE;
buf = JS_GetStringChars(str);
len = JS_GetStringLength(str);
#if IS_BIG_ENDIAN
{
jschar *from, *to;
uint32 i;
/* Swap bytes in Unichars to keep frozen strings machine-independent. */
from = (jschar *)buf;
to = (jschar *) JS_malloc(cx, len * sizeof(jschar));
if (!to) {
JS_XDRDestroy(xdr);
return JS_FALSE;
}
for (i = 0; i < len; i++)
to[i] = JSXDR_SWAB16(from[i]);
buf = (char *)to;
}
#endif
len *= sizeof(jschar);
JS_XDRMemSetData(xdr, buf, len);
/* XXXbe should magic mismatch be error, or false return value? */
ok = js_XDRScript(xdr, &script, &hasMagic);
if (!ok)
goto out;
if (!hasMagic) {
*rval = JSVAL_FALSE;
goto out;
}
JS_LOCK_OBJ(cx, obj);
execDepth = GetScriptExecDepth(cx, obj);
/*
* execDepth must be 0 to allow compilation here, otherwise the JSScript
* struct can be released while running.
*/
if (execDepth > 0) {
JS_UNLOCK_OBJ(cx, obj);
JS_ReportErrorNumber(cx, js_GetErrorMessage, NULL,
JSMSG_COMPILE_EXECED_SCRIPT);
goto out;
}
/* Swap script for obj's old script, if any. */
v = LOCKED_OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
oldscript = !JSVAL_IS_VOID(v) ? (JSScript *) JSVAL_TO_PRIVATE(v) : NULL;
LOCKED_OBJ_SET_SLOT(cx, obj, JSSLOT_PRIVATE, PRIVATE_TO_JSVAL(script));
JS_UNLOCK_OBJ(cx, obj);
if (oldscript)
js_DestroyScript(cx, oldscript);
script->object = obj;
js_CallNewScriptHook(cx, script, NULL);
out:
/*
* We reset the buffer to be NULL so that it doesn't free the chars
* memory owned by str (argv[0]).
*/
JS_XDRMemSetData(xdr, NULL, 0);
JS_XDRDestroy(xdr);
#if IS_BIG_ENDIAN
JS_free(cx, buf);
#endif
*rval = JSVAL_TRUE;
return ok;
}
static const char js_thaw_str[] = "thaw";
#endif /* JS_HAS_XDR_FREEZE_THAW */
#endif /* JS_HAS_XDR */
static JSFunctionSpec script_methods[] = {
#if JS_HAS_TOSOURCE
{js_toSource_str, script_toSource, 0,0,0},
#endif
{js_toString_str, script_toString, 0,0,0},
{"compile", script_compile, 2,0,0},
{"exec", script_exec, 1,0,0},
#if JS_HAS_XDR_FREEZE_THAW
{"freeze", script_freeze, 0,0,0},
{js_thaw_str, script_thaw, 1,0,0},
#endif /* JS_HAS_XDR_FREEZE_THAW */
{0,0,0,0,0}
};
#endif /* JS_HAS_SCRIPT_OBJECT */
static void
script_finalize(JSContext *cx, JSObject *obj)
{
JSScript *script;
script = (JSScript *) JS_GetPrivate(cx, obj);
if (script)
js_DestroyScript(cx, script);
}
static JSBool
script_call(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
#if JS_HAS_SCRIPT_OBJECT
return script_exec(cx, JSVAL_TO_OBJECT(argv[-2]), argc, argv, rval);
#else
return JS_FALSE;
#endif
}
static uint32
script_mark(JSContext *cx, JSObject *obj, void *arg)
{
JSScript *script;
script = (JSScript *) JS_GetPrivate(cx, obj);
if (script)
js_MarkScript(cx, script);
return 0;
}
#if !JS_HAS_SCRIPT_OBJECT
const char js_Script_str[] = "Script";
#define JSProto_Script JSProto_Object
#endif
JS_FRIEND_DATA(JSClass) js_ScriptClass = {
js_Script_str,
JSCLASS_HAS_PRIVATE | JSCLASS_HAS_CACHED_PROTO(JSProto_Script) |
JSCLASS_HAS_RESERVED_SLOTS(1),
JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, script_finalize,
NULL, NULL, script_call, NULL,/*XXXbe xdr*/
NULL, NULL, script_mark, 0
};
#if JS_HAS_SCRIPT_OBJECT
static JSBool
Script(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
/* If not constructing, replace obj with a new Script object. */
if (!(cx->fp->flags & JSFRAME_CONSTRUCTING)) {
obj = js_NewObject(cx, &js_ScriptClass, NULL, NULL);
if (!obj)
return JS_FALSE;
/*
* script_compile does not use rval to root its temporaries
* so we can use it to root obj.
*/
*rval = OBJECT_TO_JSVAL(obj);
}
if (!JS_SetReservedSlot(cx, obj, 0, INT_TO_JSVAL(0)))
return JS_FALSE;
return script_compile(cx, obj, argc, argv, rval);
}
#if JS_HAS_XDR_FREEZE_THAW
static JSBool
script_static_thaw(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
jsval *rval)
{
obj = js_NewObject(cx, &js_ScriptClass, NULL, NULL);
if (!obj)
return JS_FALSE;
if (!script_thaw(cx, obj, argc, argv, rval))
return JS_FALSE;
*rval = OBJECT_TO_JSVAL(obj);
return JS_TRUE;
}
static JSFunctionSpec script_static_methods[] = {
{js_thaw_str, script_static_thaw, 1,0,0},
{0,0,0,0,0}
};
#else /* !JS_HAS_XDR_FREEZE_THAW */