<commit_before>/*
 * evaluate.js
 * Objective-J
 *
 * Created by <NAME>.
 * Copyright 2008, 280 North, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

var objj_included_files = { };  
    
var FRAGMENT_CODE   = 1,

    FRAGMENT_FILE   = 1 << 2,
    FRAGMENT_LOCAL  = 1 << 3;
    
function objj_fragment()
{
    this.info       = NULL;
    this.type       = 0;
    this.context    = NULL;
    this.bundle     = NULL;
    this.file       = NULL;
}

#define EVALUATION_PERIOD 3000

function objj_context()
{
    this.fragments  = [];
    this.scheduled  = NO;
    this.blocked    = NO;
}

#define SET_CONTEXT(aFragment, aContext) aFragment.context = aContext
#define GET_CONTEXT(aFragment) aFragment.context

#define SET_TYPE(aFragment, aType) aFragment.type = (aType)
#define GET_TYPE(aFragment) aFragment.type

#define GET_CODE(aFragment) aFragment.info
#define SET_CODE(aFragment, aCode) aFragment.info = (aCode)

#define GET_PATH(aFragment) aFragment.info
#define SET_PATH(aFragment, aPath) aFragment.info = aPath

#define GET_BUNDLE(aFragment) aFragment.bundle
#define SET_BUNDLE(aFragment, aBundle) aFragment.bundle = aBundle

#define GET_FILE(aFragment) aFragment.file
#define SET_FILE(aFragment, aFile) aFragment.file = aFile

#define IS_FILE(aFragment) (aFragment.type & FRAGMENT_FILE)
#define IS_LOCAL(aFragment) (aFragment.type & FRAGMENT_LOCAL)

objj_fragment.prototype.toMarkedString = function()
{
    return IS_FILE(this) ?  (IS_LOCAL(this) ? MARKER_IMPORT_LOCAL : MARKER_IMPORT_STD) + ';' + GET_PATH(this).length + ';' + GET_PATH(this) :
                            MARKER_CODE + ';' + GET_CODE(this).length + ';' + GET_CODE(this);
}

function fragment_create_code(aCode, aBundle, aFile)
{
    var fragment = new objj_fragment();
    
    SET_TYPE(fragment, FRAGMENT_CODE);
    SET_CODE(fragment, aCode);
    SET_BUNDLE(fragment, aBundle);
    SET_FILE(fragment, aFile);
    
    return fragment;
}

function fragment_create_file(aPath, aBundle, isLocal, aFile)
{
    var fragment = new objj_fragment();
    
    SET_TYPE(fragment, FRAGMENT_FILE | (FRAGMENT_LOCAL * isLocal));
    SET_PATH(fragment, aPath);
    SET_BUNDLE(fragment, aBundle);
    SET_FILE(fragment, aFile);
    
    return fragment;
}

objj_context.prototype.evaluate = function()
{
    this.scheduled = NO;

    // If we're blocked by IO, then just reschedule.
    if (this.blocked)
        return this.schedule();
    
    // If not, begin the evaluation loop.    
    var sleep = NO,
        start = new Date(),
        fragments = this.fragments;
    
    while (!sleep && fragments.length)
    {
        var fragment = fragments.pop();
            
        if (IS_FILE(fragment))
            sleep = fragment_evaluate_file(fragment);
        else
            sleep = fragment_evaluate_code(fragment);
        
        // Sleep evaluation if we've been at it too long to avoid 
        // unresponsive script errors.
        sleep = sleep || ((new Date() - start) > EVALUATION_PERIOD);
    }
    
    if (sleep)
        this.schedule();
    
    else if (this.didCompleteCallback)
        this.didCompleteCallback(this);
}

objj_context.prototype.schedule = function()
{
    if (this.scheduled)
        return;
    
    this.scheduled = YES;
    
    var context = this;
    
    window.setNativeTimeout(function () { context.evaluate(); }, 0);
}

objj_context.prototype.pushFragment = function(aFragment)
{
    SET_CONTEXT(aFragment, this);
    
    this.fragments.push(aFragment);
}

function fragment_evaluate_code(aFragment)
{
    var compiled;
    
    OBJJ_CURRENT_BUNDLE = GET_BUNDLE(aFragment);
    
    try
    {
#if RHINO
        var functionText = "function(){"+GET_CODE(aFragment)+"/**/\n}";
        if (system.engine === "rhino")
            compiled = Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(window, functionText, GET_FILE(aFragment).path, 0, null);
        else
            compiled = eval(functionText);<commit_msg>Check for "system", fixes press.<commit_after>/*
 * evaluate.js
 * Objective-J
 *
 * Created by <NAME>.
 * Copyright 2008, 280 North, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

var objj_included_files = { };  
    
var FRAGMENT_CODE   = 1,

    FRAGMENT_FILE   = 1 << 2,
    FRAGMENT_LOCAL  = 1 << 3;
    
function objj_fragment()
{
    this.info       = NULL;
    this.type       = 0;
    this.context    = NULL;
    this.bundle     = NULL;
    this.file       = NULL;
}

#define EVALUATION_PERIOD 3000

function objj_context()
{
    this.fragments  = [];
    this.scheduled  = NO;
    this.blocked    = NO;
}

#define SET_CONTEXT(aFragment, aContext) aFragment.context = aContext
#define GET_CONTEXT(aFragment) aFragment.context

#define SET_TYPE(aFragment, aType) aFragment.type = (aType)
#define GET_TYPE(aFragment) aFragment.type

#define GET_CODE(aFragment) aFragment.info
#define SET_CODE(aFragment, aCode) aFragment.info = (aCode)

#define GET_PATH(aFragment) aFragment.info
#define SET_PATH(aFragment, aPath) aFragment.info = aPath

#define GET_BUNDLE(aFragment) aFragment.bundle
#define SET_BUNDLE(aFragment, aBundle) aFragment.bundle = aBundle

#define GET_FILE(aFragment) aFragment.file
#define SET_FILE(aFragment, aFile) aFragment.file = aFile

#define IS_FILE(aFragment) (aFragment.type & FRAGMENT_FILE)
#define IS_LOCAL(aFragment) (aFragment.type & FRAGMENT_LOCAL)

objj_fragment.prototype.toMarkedString = function()
{
    return IS_FILE(this) ?  (IS_LOCAL(this) ? MARKER_IMPORT_LOCAL : MARKER_IMPORT_STD) + ';' + GET_PATH(this).length + ';' + GET_PATH(this) :
                            MARKER_CODE + ';' + GET_CODE(this).length + ';' + GET_CODE(this);
}

function fragment_create_code(aCode, aBundle, aFile)
{
    var fragment = new objj_fragment();
    
    SET_TYPE(fragment, FRAGMENT_CODE);
    SET_CODE(fragment, aCode);
    SET_BUNDLE(fragment, aBundle);
    SET_FILE(fragment, aFile);
    
    return fragment;
}

function fragment_create_file(aPath, aBundle, isLocal, aFile)
{
    var fragment = new objj_fragment();
    
    SET_TYPE(fragment, FRAGMENT_FILE | (FRAGMENT_LOCAL * isLocal));
    SET_PATH(fragment, aPath);
    SET_BUNDLE(fragment, aBundle);
    SET_FILE(fragment, aFile);
    
    return fragment;
}

objj_context.prototype.evaluate = function()
{
    this.scheduled = NO;

    // If we're blocked by IO, then just reschedule.
    if (this.blocked)
        return this.schedule();
    
    // If not, begin the evaluation loop.    
    var sleep = NO,
        start = new Date(),
        fragments = this.fragments;
    
    while (!sleep && fragments.length)
    {
        var fragment = fragments.pop();
            
        if (IS_FILE(fragment))
            sleep = fragment_evaluate_file(fragment);
        else
            sleep = fragment_evaluate_code(fragment);
        
        // Sleep evaluation if we've been at it too long to avoid 
        // unresponsive script errors.
        sleep = sleep || ((new Date() - start) > EVALUATION_PERIOD);
    }
    
    if (sleep)
        this.schedule();
    
    else if (this.didCompleteCallback)
        this.didCompleteCallback(this);
}

objj_context.prototype.schedule = function()
{
    if (this.scheduled)
        return;
    
    this.scheduled = YES;
    
    var context = this;
    
    window.setNativeTimeout(function () { context.evaluate(); }, 0);
}

objj_context.prototype.pushFragment = function(aFragment)
{
    SET_CONTEXT(aFragment, this);
    
    this.fragments.push(aFragment);
}

function fragment_evaluate_code(aFragment)
{
    var compiled;
    
    OBJJ_CURRENT_BUNDLE = GET_BUNDLE(aFragment);
    
    try
    {
#if RHINO
        var functionText = "function(){"+GET_CODE(aFragment)+"/**/\n}";
        if (typeof system !== "undefined" && system.engine === "rhino")
            compiled = Packages.org.mozilla.javascript.Context.getCurrentContext().compileFunction(window, functionText, GET_FILE(aFragment).path, 0, null);
        else
            compiled = eval(functionText);
{'fraction_non_alphanumeric': 0.10480769230769231, 'fraction_numerical': 0.006837606837606838, 'mean_word_length': 3.129245699161888, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7355071', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2917, 'n_words': 764}