3 # This script modifies C code to use the hijacked NSPR routines that are
4 # now baked into the JavaScript engine rather than using the NSPR
5 # routines that they were based on, i.e. types like PRArenaPool are changed
8 # This script was used in 9/98 to facilitate the incorporation of some NSPR
9 # code into the JS engine so as to minimize dependency on NSPR.
12 # Command-line: jsify.pl [options] [filename]*
15 # -r Reverse direction of transformation, i.e. JS ==> NSPR2
16 # -outdir Directory in which to place output files
19 # NSPR2 symbols that will be modified to JS symbols, e.g.
20 # PRArena <==> JSArena
30 "PR_ARENA_CONST_ALIGN_MASK",
31 "PR_ARENA_DEFAULT_ALIGN",
58 "PR_PostSynchronousEvent",
59 "PR_ProcessPendingEvents",
60 "PR_CreateEventQueue",
63 "PR_DestroyEventQueue",
68 "PR_GetEventQueueMonitor",
69 "PR_GetEventQueueSelectFD",
70 "PR_GetMainEventQueue",
73 "PR_ENTER_EVENT_QUEUE_MONITOR",
74 "PR_EXIT_EVENT_QUEUE_MONITOR",
95 "PR_HashTableDestroy",
97 "PR_HashTableEnumerateEntries",
100 "PR_HashTableRawLookup",
101 "PR_HashTableRawRemove",
102 "PR_HashTableRemove",
128 "PR_ALIGN_OF_DOUBLE",
133 "PR_ALIGN_OF_POINTER",
137 "PR_BITS_PER_BYTE_LOG2",
138 "PR_BITS_PER_DOUBLE",
139 "PR_BITS_PER_DOUBLE_LOG2",
141 "PR_BITS_PER_FLOAT_LOG2",
144 "PR_BITS_PER_INT64_LOG2",
145 "PR_BITS_PER_INT_LOG2",
147 "PR_BITS_PER_LONG_LOG2",
149 "PR_BITS_PER_SHORT_LOG2",
151 "PR_BITS_PER_WORD_LOG2",
153 "PR_BYTES_PER_DOUBLE",
154 "PR_BYTES_PER_DWORD",
155 "PR_BYTES_PER_DWORD_LOG2",
156 "PR_BYTES_PER_FLOAT",
158 "PR_BYTES_PER_INT64",
160 "PR_BYTES_PER_SHORT",
162 "PR_BYTES_PER_WORD_LOG2",
173 "PR_ATOMIC_DWORD_LOAD",
174 "PR_ATOMIC_DWORD_STORE",
179 "PR_ArenaCountAllocation",
180 "PR_ArenaCountGrowth",
181 "PR_ArenaCountInplaceGrowth",
182 "PR_ArenaCountRelease",
183 "PR_ArenaCountRetract",
187 "PR_CompactArenaPool",
189 "PR_FinishArenaPool",
203 "PR_BUFFER_OVERFLOW_ERROR",
216 "PR_GetSegmentAccess",
218 "PR_GetSegmentVaddr",
248 "PR_INIT_ARENA_POOL",
250 "PR_INIT_STATIC_CLIST",
256 "PR_INTERVAL_NO_TIMEOUT",
257 "PR_INTERVAL_NO_WAIT",
298 "PR_REMOVE_AND_INIT_LINK",
304 "PR_STATIC_CALLBACK",
307 "PR_SetLogBuffering",
343 while ($ARGV[0] =~ /^-/) {
344 if ($ARGV[0] eq "-r") {
346 $reverse_conversion = 1;
347 } elsif ($ARGV[0] eq "-outdir") {
353 # Given an NSPR symbol compute the JS equivalent or
356 local ($replacement);
359 $replacement = substr($sym,0,2) eq "pr" ?
"js" : "JS";
360 $replacement .= substr($sym, 2);
364 # Build the regular expression that will convert between the NSPR
365 # types and the JS types
366 if ($reverse_conversion) {
367 die "Not implemented yet";
369 foreach $sym (@NSPR_symbols) {
370 $regexp .= $sym . "|"
372 # Get rid of the last "!"
375 # Replace PR* with JS* and replace pr* with js*
376 $regexp = 's/(^|\\W)(' . $regexp . ')/$1 . &subst($2)/eg';
380 # Pre-compile a little subroutine to perform the regexp substitution
381 # between NSPR types and JS types
382 eval('sub convert_from_NSPR {($line) = @_; $line =~ ' . $regexp . ';}');
384 sub convert_mallocs
{
386 $line =~ s/PR_MALLOC/malloc/g;
387 $line =~ s/PR_REALLOC/realloc/g;
388 $line =~ s/PR_FREE/free/g;
392 sub convert_includes
{
394 if ($line !~ /include/) {
398 if ($line =~ /prlog\.h/) {
399 $line = '#include "jsutil.h"'. " /* Added by JSIFY */\n";
400 } elsif ($line =~ /plhash\.h/) {
401 $line = '#include "jshash.h"'. " /* Added by JSIFY */\n";
402 } elsif ($line =~ /plarena\.h/) {
403 $line = '#include "jsarena.h"'. " /* Added by JSIFY */\n";
404 } elsif ($line =~ /prmem\.h/) {
406 } elsif ($line =~ /jsmsg\.def/) {
407 $line = '#include "js.msg"' . "\n";
408 } elsif ($line =~ /shellmsg\.def/) {
409 $line = '#include "jsshell.msg"' . "\n";
410 } elsif ($line =~ /jsopcode\.def/) {
411 $line = '#include "jsopcode.tbl"' . "\n";
416 sub convert_declarations
{
418 $line =~ s/PR_EXTERN/JS_EXTERN_API/g;
419 $line =~ s/PR_IMPLEMENT_DATA/JS_EXPORT_DATA/g;
420 $line =~ s/PR_IMPLEMENT/JS_EXPORT_API/g;
421 $line =~ s/PR_IMPORT/JS_IMPORT/g;
422 $line =~ s/PR_PUBLIC_API/JS_EXPORT_API/g;
423 $line =~ s/PR_PUBLIC_DATA/JS_EXPORT_DATA/g;
427 sub convert_long_long_macros
{
429 $line =~ s/\b(LL_)/JSLL_/g;
433 sub convert_asserts
{
435 $line =~ s/\bPR_ASSERT/JS_ASSERT/g;
439 while ($#ARGV >= 0) {
442 # Change filename, e.g. prtime.h to jsprtime.h, except for legacy
443 # files that start with 'prmj', like prmjtime.h.
445 if ($infile !~ /^prmj/) {
446 $outfile =~ s/^pr/js/;
447 $outfile =~ s/^pl/js/;
451 $outfile = $outdir . '/' . $outfile;
454 if ($infile eq $outfile) {
455 die "Error: refuse to overwrite $outfile, use -outdir option."
457 die "Can't open $infile" if !open(INFILE
, "<$infile");
458 die "Can't open $outfile for writing" if !open(OUTFILE
, ">$outfile");
463 #Get rid of #include "prlog.h"
464 &convert_includes
($line);
466 # Rename PR_EXTERN, PR_IMPORT, etc.
467 &convert_declarations
($line);
469 # Convert from PR_MALLOC to malloc, etc.
470 &convert_mallocs
($line);
472 # Convert from PR_ASSERT to JS_ASSERT
473 # &convert_asserts($line);
475 # Convert from, e.g. PRArena to JSPRArena
476 &convert_from_NSPR
($line);
478 # Change LL_* macros to JSLL_*
479 &convert_long_long_macros
($line);