Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
jsd_text.c File Reference
#include <ctype.h>
#include "jsd.h"

Go to the source code of this file.

Defines

#define MEMBUF_GROW   1000
#define FILE_URL_PREFIX_LEN   (sizeof file_url_prefix - 1)
#define DEBUG_ITERATE_SOURCES(x)   ((void)x)
#define UNICODE_TRUNCATE_BUF_SIZE   1024

Functions

static void _clearText (JSDContext *jsdc, JSDSourceText *jsdsrc)
static JSBool _appendText (JSDContext *jsdc, JSDSourceText *jsdsrc, const char *text, size_t length)
static JSDSourceText_newSource (JSDContext *jsdc, const char *url)
static void _destroySource (JSDContext *jsdc, JSDSourceText *jsdsrc)
static void _removeSource (JSDContext *jsdc, JSDSourceText *jsdsrc)
static JSDSourceText_addSource (JSDContext *jsdc, const char *url)
static void _moveSourceToFront (JSDContext *jsdc, JSDSourceText *jsdsrc)
static void _moveSourceToRemovedList (JSDContext *jsdc, JSDSourceText *jsdsrc)
static void _removeSourceFromRemovedList (JSDContext *jsdc, JSDSourceText *jsdsrc)
static JSBool _isSourceInSourceList (JSDContext *jsdc, JSDSourceText *jsdsrcToFind)
static int strncasecomp (const char *one, const char *two, int n)
const char * jsd_BuildNormalizedURL (const char *url_string)
void jsd_DestroyAllSources (JSDContext *jsdc)
JSDSourceTextjsd_IterateSources (JSDContext *jsdc, JSDSourceText **iterp)
JSDSourceTextjsd_FindSourceForURL (JSDContext *jsdc, const char *url)
const char * jsd_GetSourceURL (JSDContext *jsdc, JSDSourceText *jsdsrc)
JSBool jsd_GetSourceText (JSDContext *jsdc, JSDSourceText *jsdsrc, const char **ppBuf, intN *pLen)
void jsd_ClearSourceText (JSDContext *jsdc, JSDSourceText *jsdsrc)
JSDSourceStatus jsd_GetSourceStatus (JSDContext *jsdc, JSDSourceText *jsdsrc)
JSBool jsd_IsSourceDirty (JSDContext *jsdc, JSDSourceText *jsdsrc)
void jsd_SetSourceDirty (JSDContext *jsdc, JSDSourceText *jsdsrc, JSBool dirty)
uintN jsd_GetSourceAlterCount (JSDContext *jsdc, JSDSourceText *jsdsrc)
uintN jsd_IncrementSourceAlterCount (JSDContext *jsdc, JSDSourceText *jsdsrc)
JSDSourceTextjsd_NewSourceText (JSDContext *jsdc, const char *url)
JSDSourceTextjsd_AppendSourceText (JSDContext *jsdc, JSDSourceText *jsdsrc, const char *text, size_t length, JSDSourceStatus status)
JSDSourceTextjsd_AppendUCSourceText (JSDContext *jsdc, JSDSourceText *jsdsrc, const jschar *text, size_t length, JSDSourceStatus status)
JSBool jsd_AddFullSourceText (JSDContext *jsdc, const char *text, size_t length, const char *url)
void jsd_StartingEvalUsingFilename (JSDContext *jsdc, const char *url)
void jsd_FinishedEvalUsingFilename (JSDContext *jsdc, const char *url)

Variables

static char file_url_prefix [] = "file:"

Define Documentation

#define DEBUG_ITERATE_SOURCES (   x)    ((void)x)

Definition at line 372 of file jsd_text.c.

Definition at line 214 of file jsd_text.c.

#define MEMBUF_GROW   1000

Function Documentation

static JSDSourceText* _addSource ( JSDContext jsdc,
const char *  url 
) [static]

Definition at line 145 of file jsd_text.c.

{
    JSDSourceText* jsdsrc = _newSource(jsdc, url);
    if( ! jsdsrc )
        return NULL;
    JS_INSERT_LINK(&jsdsrc->links, &jsdc->sources);
    return jsdsrc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static JSBool _appendText ( JSDContext jsdc,
JSDSourceText jsdsrc,
const char *  text,
size_t  length 
) [static]

Definition at line 71 of file jsd_text.c.

{
#define MEMBUF_GROW 1000

    uintN neededSize = jsdsrc->textLength + length;

    if( neededSize > jsdsrc->textSpace )
    {
        char* newBuf;
        uintN iNewSize;

        /* if this is the first alloc, the req might be all that's needed*/
        if( ! jsdsrc->textSpace )
            iNewSize = length;
        else
            iNewSize = (neededSize * 5 / 4) + MEMBUF_GROW;

        newBuf = (char*) realloc(jsdsrc->text, iNewSize);
        if( ! newBuf )
        {
            /* try again with the minimal size really asked for */
            iNewSize = neededSize;
            newBuf = (char*) realloc(jsdsrc->text, iNewSize);
            if( ! newBuf )
            {
                /* out of memory */
                _clearText( jsdc, jsdsrc );
                jsdsrc->status = JSD_SOURCE_FAILED;
                return JS_FALSE;
            }
        }

        jsdsrc->text = newBuf;
        jsdsrc->textSpace = iNewSize;
    }

    memcpy(jsdsrc->text + jsdsrc->textLength, text, length);
    jsdsrc->textLength += length;
    return JS_TRUE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void _clearText ( JSDContext jsdc,
JSDSourceText jsdsrc 
) [static]

Definition at line 57 of file jsd_text.c.

{
    if( jsdsrc->text )
        free(jsdsrc->text);
    jsdsrc->text        = NULL;
    jsdsrc->textLength  = 0;
    jsdsrc->textSpace   = 0;
    jsdsrc->status      = JSD_SOURCE_CLEARED;
    jsdsrc->dirty       = JS_TRUE;
    jsdsrc->alterCount  = jsdc->sourceAlterCount++ ;
    jsdsrc->doingEval   = JS_FALSE;
}    

Here is the caller graph for this function:

static void _destroySource ( JSDContext jsdc,
JSDSourceText jsdsrc 
) [static]

Definition at line 129 of file jsd_text.c.

{
    JS_ASSERT(NULL == jsdsrc->text);  /* must _clearText() first */
    free(jsdsrc->url);
    free(jsdsrc);
}

Here is the caller graph for this function:

static JSBool _isSourceInSourceList ( JSDContext jsdc,
JSDSourceText jsdsrcToFind 
) [static]

Definition at line 177 of file jsd_text.c.

{
    JSDSourceText *jsdsrc;

    for( jsdsrc = (JSDSourceText*)jsdc->sources.next;
         jsdsrc != (JSDSourceText*)&jsdc->sources;
         jsdsrc = (JSDSourceText*)jsdsrc->links.next ) 
    {
        if( jsdsrc == jsdsrcToFind )
            return JS_TRUE;
    }
    return JS_FALSE;
}

Here is the caller graph for this function:

static void _moveSourceToFront ( JSDContext jsdc,
JSDSourceText jsdsrc 
) [static]

Definition at line 155 of file jsd_text.c.

{
    JS_REMOVE_LINK(&jsdsrc->links);
    JS_INSERT_LINK(&jsdsrc->links, &jsdc->sources);
}
static void _moveSourceToRemovedList ( JSDContext jsdc,
JSDSourceText jsdsrc 
) [static]

Definition at line 162 of file jsd_text.c.

{
    _clearText(jsdc, jsdsrc);
    JS_REMOVE_LINK(&jsdsrc->links);
    JS_INSERT_LINK(&jsdsrc->links, &jsdc->removedSources);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static JSDSourceText* _newSource ( JSDContext jsdc,
const char *  url 
) [static]

Definition at line 114 of file jsd_text.c.

{
    JSDSourceText* jsdsrc = (JSDSourceText*)calloc(1,sizeof(JSDSourceText));
    if( ! jsdsrc )
        return NULL;
    
    jsdsrc->url        = (char*) url; /* already a copy */
    jsdsrc->status     = JSD_SOURCE_INITED;
    jsdsrc->dirty      = JS_TRUE;
    jsdsrc->alterCount = jsdc->sourceAlterCount++ ;
            
    return jsdsrc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void _removeSource ( JSDContext jsdc,
JSDSourceText jsdsrc 
) [static]

Definition at line 137 of file jsd_text.c.

{
    JS_REMOVE_LINK(&jsdsrc->links);
    _clearText(jsdc, jsdsrc);
    _destroySource(jsdc, jsdsrc);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void _removeSourceFromRemovedList ( JSDContext jsdc,
JSDSourceText jsdsrc 
) [static]

Definition at line 170 of file jsd_text.c.

{
    JS_REMOVE_LINK(&jsdsrc->links);
    _destroySource( jsdc, jsdsrc );
}

Here is the call graph for this function:

Here is the caller graph for this function:

JSBool jsd_AddFullSourceText ( JSDContext jsdc,
const char *  text,
size_t  length,
const char *  url 
)

Definition at line 499 of file jsd_text.c.

{
    JSDSourceText* jsdsrc;

    JSD_LOCK_SOURCE_TEXT(jsdc);

    jsdsrc = jsd_NewSourceText(jsdc, url);
    if( jsdsrc )
        jsdsrc = jsd_AppendSourceText(jsdc, jsdsrc,
                                      text, length, JSD_SOURCE_PARTIAL );
    if( jsdsrc )
        jsdsrc = jsd_AppendSourceText(jsdc, jsdsrc,
                                      NULL, 0, JSD_SOURCE_COMPLETED );

    JSD_UNLOCK_SOURCE_TEXT(jsdc);

    return jsdsrc ? JS_TRUE : JS_FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JSDSourceText* jsd_AppendSourceText ( JSDContext jsdc,
JSDSourceText jsdsrc,
const char *  text,
size_t  length,
JSDSourceStatus  status 
)

Definition at line 417 of file jsd_text.c.

{
    JSD_LOCK_SOURCE_TEXT(jsdc);

    if( jsdsrc->doingEval )
    {
        JSD_UNLOCK_SOURCE_TEXT(jsdc);
        return NULL;
    }

    if( ! _isSourceInSourceList( jsdc, jsdsrc ) )
    {
        _removeSourceFromRemovedList( jsdc, jsdsrc );
        JSD_UNLOCK_SOURCE_TEXT(jsdc);
        return NULL;
    }

    if( text && length && ! _appendText( jsdc, jsdsrc, text, length ) )
    {
        jsdsrc->dirty  = JS_TRUE;
        jsdsrc->alterCount  = jsdc->sourceAlterCount++ ;
        jsdsrc->status = JSD_SOURCE_FAILED;
        _moveSourceToRemovedList(jsdc, jsdsrc);
        JSD_UNLOCK_SOURCE_TEXT(jsdc);
        return NULL;    
    }

    jsdsrc->dirty  = JS_TRUE;
    jsdsrc->alterCount  = jsdc->sourceAlterCount++ ;
    jsdsrc->status = status;
    DEBUG_ITERATE_SOURCES(jsdc);
    JSD_UNLOCK_SOURCE_TEXT(jsdc);
    return jsdsrc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

JSDSourceText* jsd_AppendUCSourceText ( JSDContext jsdc,
JSDSourceText jsdsrc,
const jschar text,
size_t  length,
JSDSourceStatus  status 
)

Definition at line 457 of file jsd_text.c.

{
#define UNICODE_TRUNCATE_BUF_SIZE 1024
    static char* buf = NULL;
    int remaining = length;

    if(!text || !length)
        return jsd_AppendSourceText(jsdc, jsdsrc, NULL, 0, status);

    JSD_LOCK_SOURCE_TEXT(jsdc);
    if(!buf)
    {
        buf = malloc(UNICODE_TRUNCATE_BUF_SIZE);
        if(!buf)
        {
            JSD_UNLOCK_SOURCE_TEXT(jsdc);
            return NULL;
        }
    }
    while(remaining && jsdsrc) {
        int bytes = JS_MIN(remaining, UNICODE_TRUNCATE_BUF_SIZE);
        int i;
        for(i = 0; i < bytes; i++)
            buf[i] = (const char) *(text++);
        jsdsrc = jsd_AppendSourceText(jsdc,jsdsrc,
                                      buf, bytes,
                                      JSD_SOURCE_PARTIAL);
        remaining -= bytes;
    }
    if(jsdsrc && status != JSD_SOURCE_PARTIAL)
        jsdsrc = jsd_AppendSourceText(jsdc, jsdsrc, NULL, 0, status);

    JSD_UNLOCK_SOURCE_TEXT(jsdc);
    return jsdsrc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const char* jsd_BuildNormalizedURL ( const char *  url_string)

Definition at line 217 of file jsd_text.c.

{
    char *new_url_string;

    if( ! url_string )
        return NULL;

    if (!strncasecomp(url_string, file_url_prefix, FILE_URL_PREFIX_LEN) &&
        url_string[FILE_URL_PREFIX_LEN + 0] == '/' &&
        url_string[FILE_URL_PREFIX_LEN + 1] == '/') {
        new_url_string = JS_smprintf("%s%s",
                                     file_url_prefix,
                                     url_string + FILE_URL_PREFIX_LEN + 2);
    } else {
        new_url_string = strdup(url_string);
    }
    return new_url_string;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void jsd_ClearSourceText ( JSDContext jsdc,
JSDSourceText jsdsrc 
)

Definition at line 306 of file jsd_text.c.

{
    if( JSD_SOURCE_INITED  != jsdsrc->status &&
        JSD_SOURCE_PARTIAL != jsdsrc->status )
    {
        _clearText(jsdc, jsdsrc);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 239 of file jsd_text.c.

{
    JSDSourceText *jsdsrc;
    JSDSourceText *next;

    for( jsdsrc = (JSDSourceText*)jsdc->sources.next;
         jsdsrc != (JSDSourceText*)&jsdc->sources;
         jsdsrc = next ) 
    {
        next = (JSDSourceText*)jsdsrc->links.next;
        _removeSource( jsdc, jsdsrc );
    }

    for( jsdsrc = (JSDSourceText*)jsdc->removedSources.next;
         jsdsrc != (JSDSourceText*)&jsdc->removedSources;
         jsdsrc = next ) 
    {
        next = (JSDSourceText*)jsdsrc->links.next;
        _removeSourceFromRemovedList( jsdc, jsdsrc );
    }

}

Here is the call graph for this function:

Here is the caller graph for this function:

JSDSourceText* jsd_FindSourceForURL ( JSDContext jsdc,
const char *  url 
)

Definition at line 276 of file jsd_text.c.

{
    JSDSourceText *jsdsrc;

    for( jsdsrc = (JSDSourceText *)jsdc->sources.next;
         jsdsrc != (JSDSourceText *)&jsdc->sources;
         jsdsrc = (JSDSourceText *)jsdsrc->links.next )
    {
        if( 0 == strcmp(jsdsrc->url, url) )
            return jsdsrc;
    }
    return NULL;
}

Here is the caller graph for this function:

void jsd_FinishedEvalUsingFilename ( JSDContext jsdc,
const char *  url 
)

Definition at line 544 of file jsd_text.c.

{
    JSDSourceText* jsdsrc;

    /* NOTE: We ASSUME it is locked! */

    jsdsrc = jsd_FindSourceForURL(jsdc, url);
    if(jsdsrc)
    {
#if 0
#ifndef JSD_LOWLEVEL_SOURCE
        /*
        * when using this low level source addition, this jsdsrc might 
        * not have existed before the eval, but does exist now (without
        * this flag set!)
        */
        JS_ASSERT(jsdsrc->doingEval);
#endif
#endif
        jsdsrc->doingEval = JS_FALSE;
    }

    JSD_UNLOCK_SOURCE_TEXT(jsdc);
}    

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 334 of file jsd_text.c.

{
    return jsdsrc->alterCount;
}

Here is the caller graph for this function:

Definition at line 316 of file jsd_text.c.

{
    return jsdsrc->status;
}

Here is the caller graph for this function:

JSBool jsd_GetSourceText ( JSDContext jsdc,
JSDSourceText jsdsrc,
const char **  ppBuf,
intN pLen 
)

Definition at line 297 of file jsd_text.c.

{
    *ppBuf = jsdsrc->text;
    *pLen  = jsdsrc->textLength;
    return JS_TRUE;
}

Here is the caller graph for this function:

const char* jsd_GetSourceURL ( JSDContext jsdc,
JSDSourceText jsdsrc 
)

Definition at line 291 of file jsd_text.c.

{
    return jsdsrc->url;
}

Here is the caller graph for this function:

Definition at line 340 of file jsd_text.c.

{
    return jsdsrc->alterCount = jsdc->sourceAlterCount++;
}

Here is the caller graph for this function:

JSBool jsd_IsSourceDirty ( JSDContext jsdc,
JSDSourceText jsdsrc 
)

Definition at line 322 of file jsd_text.c.

{
    return jsdsrc->dirty;
}

Here is the caller graph for this function:

Definition at line 263 of file jsd_text.c.

{
    JSDSourceText *jsdsrc = *iterp;
    
    if( !jsdsrc )
        jsdsrc = (JSDSourceText *)jsdc->sources.next;
    if( jsdsrc == (JSDSourceText *)&jsdc->sources )
        return NULL;
    *iterp = (JSDSourceText *)jsdsrc->links.next;
    return jsdsrc;
}

Here is the caller graph for this function:

JSDSourceText* jsd_NewSourceText ( JSDContext jsdc,
const char *  url 
)

Definition at line 378 of file jsd_text.c.

{
    JSDSourceText* jsdsrc;
    const char* new_url_string;

    JSD_LOCK_SOURCE_TEXT(jsdc);

#ifdef LIVEWIRE
    new_url_string = url; /* we take ownership of alloc'd string */
#else
    new_url_string = jsd_BuildNormalizedURL(url);
#endif
    if( ! new_url_string )
        return NULL;

    jsdsrc = jsd_FindSourceForURL(jsdc, new_url_string);

    if( jsdsrc )
    {
        if( jsdsrc->doingEval )
        {
#ifdef LIVEWIRE
            free((char*)new_url_string);
#endif
            JSD_UNLOCK_SOURCE_TEXT(jsdc);
            return NULL;
        }
        else    
            _moveSourceToRemovedList(jsdc, jsdsrc);
    }

    jsdsrc = _addSource( jsdc, new_url_string );

    JSD_UNLOCK_SOURCE_TEXT(jsdc);

    return jsdsrc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void jsd_SetSourceDirty ( JSDContext jsdc,
JSDSourceText jsdsrc,
JSBool  dirty 
)

Definition at line 328 of file jsd_text.c.

{
    jsdsrc->dirty = dirty;
}

Here is the caller graph for this function:

void jsd_StartingEvalUsingFilename ( JSDContext jsdc,
const char *  url 
)

Definition at line 524 of file jsd_text.c.

{
    JSDSourceText* jsdsrc;

    /* NOTE: We leave it locked! */
    JSD_LOCK_SOURCE_TEXT(jsdc); 

    jsdsrc = jsd_FindSourceForURL(jsdc, url);
    if(jsdsrc)
    {
#if 0
#ifndef JSD_LOWLEVEL_SOURCE
        JS_ASSERT(! jsdsrc->doingEval);
#endif
#endif
        jsdsrc->doingEval = JS_TRUE;
    }
}    

Here is the call graph for this function:

Here is the caller graph for this function:

static int strncasecomp ( const char *  one,
const char *  two,
int  n 
) [static]

Definition at line 195 of file jsd_text.c.

{
    const char *pA;
    const char *pB;
    
    for(pA=one, pB=two;; pA++, pB++) 
    {
        int tmp;
        if (pA == one+n) 
            return 0;   
        if (!(*pA && *pB)) 
            return *pA - *pB;
        tmp = tolower(*pA) - tolower(*pB);
        if (tmp) 
            return tmp;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char file_url_prefix[] = "file:" [static]

Definition at line 213 of file jsd_text.c.