home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jsscript.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.4 KB  |  198 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * JS script operations.
  21.  */
  22. #include <string.h>
  23. #include "prtypes.h"
  24. #include "jsapi.h"
  25. #include "jscntxt.h"
  26. #include "jsdbgapi.h"
  27. #include "jsemit.h"
  28. #include "jsscript.h"
  29.  
  30. JSScript *
  31. js_NewScript(JSContext *cx, JSCodeGenerator *cg, const char *filename,
  32.              uintN lineno, JSPrincipals *principals, JSFunction *fun)
  33. {
  34.     JSScript *script;
  35.     ptrdiff_t length;
  36.     JSNewScriptHookProc hookproc;
  37.  
  38.     length = CG_OFFSET(cg);
  39.     script = JS_malloc(cx, sizeof(JSScript) + length);
  40.     if (!script)
  41.         return NULL;
  42.     memset(script, 0, sizeof(JSScript));
  43.     if (!js_InitAtomMap(cx, &script->atomMap, &cg->atomList)) {
  44.         JS_free(cx, script);
  45.         return NULL;
  46.     }
  47.     if (filename) {
  48.         script->filename = JS_strdup(cx, filename);
  49.         if (!script->filename) {
  50.             js_DestroyScript(cx, script);
  51.             return NULL;
  52.         }
  53.     }
  54.     script->notes = js_FinishTakingSrcNotes(cx, cg);
  55.     if (!script->notes) {
  56.         js_DestroyScript(cx, script);
  57.         return NULL;
  58.     }
  59.     script->code = (jsbytecode *)(script + 1);
  60.     memcpy(script->code, cg->base, length * sizeof(jsbytecode));
  61.     script->length = length;
  62.     script->depth = cg->maxStackDepth;
  63.     script->lineno = lineno;
  64.     script->args = cg->args;
  65.     script->vars = cg->vars;
  66.     script->javaData = 0;
  67.     if (principals)
  68.         JSPRINCIPALS_HOLD(cx, principals);
  69.     script->principals = principals;
  70.  
  71.     hookproc = (JSNewScriptHookProc)cx->runtime->newScriptHookProc;
  72.     if (hookproc) {
  73.         (*hookproc)(cx, filename, lineno, script, fun,
  74.             cx->runtime->newScriptHookProcData);
  75.     }
  76.  
  77.     return script;
  78. }
  79.  
  80. void
  81. js_DestroyScript(JSContext *cx, JSScript *script)
  82. {
  83.     JSDestroyScriptHookProc hookproc;
  84.  
  85.     hookproc = (JSDestroyScriptHookProc)cx->runtime->destroyScriptHookProc;
  86.     if (hookproc)
  87.         (*hookproc)(cx, script, cx->runtime->destroyScriptHookProcData);
  88.  
  89.     JS_ClearScriptTraps(cx, script);
  90.     js_FreeAtomMap(cx, &script->atomMap);
  91.     if (js_InterpreterHooks && js_InterpreterHooks->destroyScript)
  92.         js_InterpreterHooks->destroyScript(cx, script);
  93.     if (script->filename)
  94.         JS_free(cx, (void *)script->filename);
  95.     if (script->notes)
  96.         JS_free(cx, script->notes);
  97.     if (script->principals)
  98.         JSPRINCIPALS_DROP(cx, script->principals);
  99.     JS_free(cx, script);
  100. }
  101.  
  102. jssrcnote *
  103. js_GetSrcNote(JSScript *script, jsbytecode *pc)
  104. {
  105.     jssrcnote *sn;
  106.     ptrdiff_t offset, target;
  107.  
  108.     sn = script->notes;
  109.     if (!sn)
  110.         return NULL;
  111.     target = pc - script->code;
  112.     if ((uintN)target >= script->length)
  113.         return NULL;
  114.     for (offset = 0; !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
  115.         offset += SN_DELTA(sn);
  116.         if (offset == target && SN_IS_GETTABLE(sn))
  117.             return sn;
  118.     }
  119.     return NULL;
  120. }
  121.  
  122. uintN
  123. js_PCToLineNumber(JSScript *script, jsbytecode *pc)
  124. {
  125.     jssrcnote *sn;
  126.     ptrdiff_t offset, target;
  127.     uintN lineno;
  128.     JSSrcNoteType type;
  129.  
  130.     sn = script->notes;
  131.     if (!sn)
  132.         return 0;
  133.     target = pc - script->code;
  134.     lineno = script->lineno;
  135.     for (offset = 0; !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
  136.         offset += SN_DELTA(sn);
  137.         type = SN_TYPE(sn);
  138.         if (type == SRC_SETLINE) {
  139.             if (offset <= target)
  140.                 lineno = (uintN) js_GetSrcNoteOffset(sn, 0);
  141.         } else if (type == SRC_NEWLINE) {
  142.             if (offset <= target)
  143.                 lineno++;
  144.         }
  145.         if (offset > target)
  146.             break;
  147.     }
  148.     return lineno;
  149. }
  150.  
  151. jsbytecode *
  152. js_LineNumberToPC(JSScript *script, uintN target)
  153. {
  154.     jssrcnote *sn;
  155.     uintN lineno;
  156.     ptrdiff_t offset;
  157.     JSSrcNoteType type;
  158.  
  159.     sn = script->notes;
  160.     if (!sn)
  161.         return NULL;
  162.     lineno = script->lineno;
  163.     for (offset = 0; !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
  164.         if (lineno >= target)
  165.             break;
  166.         offset += SN_DELTA(sn);
  167.         type = SN_TYPE(sn);
  168.         if (type == SRC_SETLINE) {
  169.             lineno = (uintN) js_GetSrcNoteOffset(sn, 0);
  170.         } else if (type == SRC_NEWLINE) {
  171.             lineno++;
  172.         }
  173.     }
  174.     return script->code + offset;
  175. }
  176.  
  177. uintN
  178. js_GetScriptLineExtent(JSScript *script)
  179. {
  180.     jssrcnote *sn;
  181.     uintN lineno;
  182.     JSSrcNoteType type;
  183.  
  184.     sn = script->notes;
  185.     if (!sn)
  186.         return 0;
  187.     lineno = script->lineno;
  188.     for (; !SN_IS_TERMINATOR(sn); sn = SN_NEXT(sn)) {
  189.         type = SN_TYPE(sn);
  190.         if (type == SRC_SETLINE) {
  191.             lineno = (uintN) js_GetSrcNoteOffset(sn, 0);
  192.         } else if (type == SRC_NEWLINE) {
  193.             lineno++;
  194.         }
  195.     }
  196.     return 1 + lineno - script->lineno;
  197. }
  198.