/* -*- mode: c ; c-file-style: "canonware-c-style" -*-
 ******************************************************************************
 *
 * Copyright (C) 1996-2004 Jason Evans <jasone@canonware.com>.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice(s), this list of conditions and the following disclaimer
 *    unmodified other than the allowable addition of one or more
 *    copyright notices.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice(s), this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 *
 * Version: Onyx 5.1.1
 *
 ******************************************************************************/

#define	CW_NX_C_

#include "../include/libonyx/libonyx.h"
#include "../include/libonyx/systemdict_l.h"
#include "../include/libonyx/nx_l.h"
#include "../include/libonyx/nxa_l.h"
#include "../include/libonyx/nxo_l.h"
#include "../include/libonyx/nxo_name_l.h"

/* Prototype for automatically generated function. */
void
nx_p_nxcode(cw_nx_t *a_nx);

cw_nx_t *
nx_new(cw_nx_t *a_nx, cw_op_t *a_thread_init, cw_thread_start_t *a_thread_start)
{
    cw_nx_t *retval;
    volatile uint32_t try_stage = 0;

    xep_begin();
    volatile cw_nx_t *v_retval;
    xep_try
    {
	if (a_nx != NULL)
	{
	    retval = a_nx;
	    memset(retval, 0, sizeof(cw_nx_t));
	    retval->is_malloced = false;
	}
	else
	{
	    retval = (cw_nx_t *) cw_calloc(1, sizeof(cw_nx_t));
	    retval->is_malloced = true;
	}
	v_retval = retval;
	try_stage = 1;

#ifdef CW_DBG
	retval->magic = CW_NX_MAGIC;
#endif

	/* Set initial maximum estack depth. */
	retval->maxestack = CW_LIBONYX_ESTACK_MAX;

	/* Set initial value for tail call optimization. */
	retval->tailopt = CW_LIBONYX_TAILOPT;

	/* Initialize the internals such that if a collection happens before
	 * we're done, reference iteration will work correctly. */
	nxo_no_new(&retval->threadsdict);
	nxo_no_new(&retval->systemdict);
	nxo_no_new(&retval->globaldict);
	nxo_no_new(&retval->stdin_nxo);
	nxo_no_new(&retval->stdout_nxo);
	nxo_no_new(&retval->stderr_nxo);

	/* Insert this nx into nxa's list of nx's in the root set. */
	ql_elm_new(retval, link);
	nxa_l_nx_insert(retval);
	try_stage = 2;

	/* Initialize globaldict. */
	nxo_dict_new(&retval->globaldict, true, CW_LIBONYX_GLOBALDICT_HASH);

	/* Use stdin_nxo and stdout_nxo as temporaries for the dictionary
	 * population functions.  This is the only place where such temporaries
	 * are needed, so embedding additional fields into cw_nx_t (and
	 * reporting them during reference iteration) just for this purpose
	 * would be wasteful. */

	/* Initialize threadsdict. */
	nxo_dict_new(&retval->threadsdict, true, CW_LIBONYX_THREADSDICT_HASH);

	/* Initialize systemdict.  This must happen after the other dict
	 * initializations, since references to them are inserted into
	 * systemdict. */
	systemdict_l_populate(&retval->systemdict, &retval->stdin_nxo,
			      &retval->stdout_nxo, retval);

	/* Initialize stdin. */
	nxo_file_new(&retval->stdin_nxo, true);
#ifdef CW_POSIX_FILE
	nxo_file_fd_wrap(&retval->stdin_nxo, 0, false);
#endif
	nxo_file_origin_set(&retval->stdin_nxo,
			    "*stdin*", sizeof("*stdin*") - 1);
	
	nxo_file_buffer_size_set(&retval->stdin_nxo,
				 CW_LIBONYX_FILE_BUFFER_SIZE);

	/* Initialize stdout. */
	nxo_file_new(&retval->stdout_nxo, true);
#ifdef CW_POSIX_FILE
	nxo_file_fd_wrap(&retval->stdout_nxo, 1, false);
#endif
	nxo_file_origin_set(&retval->stdout_nxo,
			    "*stdout*", sizeof("*stdout*") - 1);
	nxo_file_buffer_size_set(&retval->stdout_nxo,
				 CW_LIBONYX_FILE_BUFFER_SIZE);

	/* Initialize stderr. */
	nxo_file_new(&retval->stderr_nxo, true);
#ifdef CW_POSIX_FILE
	nxo_file_fd_wrap(&retval->stderr_nxo, 2, false);
#endif
	nxo_file_origin_set(&retval->stderr_nxo,
			    "*stderr*", sizeof("*stderr*") - 1);

	/* Do soft operator initialization. */
	nx_p_nxcode(retval);

	/* Set the thread initialization hook pointer.  It's important to set
	 * this after doing soft operator initialization, so that the hook
	 * doesn't get called above while ininitializing globally visible soft
	 * operators. */
	retval->thread_init = a_thread_init;
    }
    xep_catch (CW_ONYXX_OOM)
    {
	retval = (cw_nx_t *) v_retval;
	switch (try_stage)
	{
	    case 2:
	    {
		nxa_l_nx_remove(retval);
	    }
	    case 1:
	    {
#ifdef CW_DBG
		memset(a_nx, 0x5a, sizeof(cw_nx_t));
#endif
		if (retval->is_malloced)
		{
		    cw_free(retval);
		}
		break;
	    }
	    default:
	    {
		cw_not_reached();
	    }
	}
    }
    xep_end();

    return retval;
}

void
nx_delete(cw_nx_t *a_nx)
{
    cw_check_ptr(a_nx);
    cw_dassert(a_nx->magic == CW_NX_MAGIC);

    nxa_l_nx_remove(a_nx);

    if (a_nx->is_malloced)
    {
	cw_free(a_nx);
    }
#ifdef CW_DBG
    else
    {
	memset(a_nx, 0x5a, sizeof(cw_nx_t));
    }
#endif
}

void
nx_maxestack_set(cw_nx_t *a_nx, cw_nxoi_t a_maxestack)
{
    cw_check_ptr(a_nx);
    cw_dassert(a_nx->magic == CW_NX_MAGIC);
    cw_assert(a_maxestack >= 0);

    a_nx->maxestack = a_maxestack;
}

void
nx_tailopt_set(cw_nx_t *a_nx, bool a_tailopt)
{
    cw_check_ptr(a_nx);
    cw_dassert(a_nx->magic == CW_NX_MAGIC);

    a_nx->tailopt = a_tailopt;
}

void
nx_stdin_set(cw_nx_t *a_nx, cw_nxo_t *a_stdin)
{
    cw_check_ptr(a_nx);
    cw_dassert(a_nx->magic == CW_NX_MAGIC);

    nxo_dup(&a_nx->stdin_nxo, a_stdin);
}

void
nx_stdout_set(cw_nx_t *a_nx, cw_nxo_t *a_stdout)
{
    cw_check_ptr(a_nx);
    cw_dassert(a_nx->magic == CW_NX_MAGIC);

    nxo_dup(&a_nx->stdout_nxo, a_stdout);
}

void
nx_stderr_set(cw_nx_t *a_nx, cw_nxo_t *a_stderr)
{
    cw_check_ptr(a_nx);
    cw_dassert(a_nx->magic == CW_NX_MAGIC);

    nxo_dup(&a_nx->stderr_nxo, a_stderr);
}
