Skip to content

Commit

Permalink
Add AS and ALIASES? natives
Browse files Browse the repository at this point in the history
Within the system there are ways to get the same series aliased as
different types.  Rather than the uphill battle of outlawing this, it
seems that the system benefits from being able to re-image a series
as another type.
  • Loading branch information
hostilefork committed Jun 19, 2016
1 parent b626c5b commit 296c3f5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/core/m-gc.c
Expand Up @@ -1351,7 +1351,6 @@ REBCNT Recycle_Core(REBOOL shutdown)
// Mark value stack (temp-saved values):
vp = SER_HEAD(REBVAL*, GC_Value_Guard);
for (n = SER_LEN(GC_Value_Guard); n > 0; n--, vp++) {
if (NOT_END(*vp))
if (NOT_END(*vp) && !IS_VOID_OR_SAFE_TRASH(*vp))
Queue_Mark_Value_Deep(*vp);
Propagate_All_GC_Marks();
Expand Down
1 change: 0 additions & 1 deletion src/core/n-control.c
Expand Up @@ -880,7 +880,6 @@ REBNATIVE(continue)
// binary! ;-- treated as UTF-8
// url! ;-- load code from URL via protocol
// file! ;-- load code from file on local disk
// group! ;-- !!! likely not needed with the AS aliasing
// tag! ;-- proposed as module library tag name, hacked as demo
// error! ;-- should use FAIL instead
// function! ;-- will only run arity 0 functions (avoids DO variadic)
Expand Down
63 changes: 63 additions & 0 deletions src/core/n-data.c
Expand Up @@ -1317,6 +1317,69 @@ REBNATIVE(punctuates_q)
}


//
// as: native [
//
// {Aliases the underlying data of one series to act as another of same class}
//
// type [datatype!]
// value [any-series!]
// ]
//
REBNATIVE(as)
{
PARAM(1, type);
PARAM(2, value);

enum Reb_Kind kind = VAL_TYPE_KIND(ARG(type));
REBVAL *value = ARG(value);

switch (kind) {
case REB_BLOCK:
case REB_GROUP:
case REB_PATH:
case REB_LIT_PATH:
case REB_GET_PATH:
if (!ANY_ARRAY(value))
fail (Error_Invalid_Arg(value));
break;

case REB_STRING:
case REB_TAG:
case REB_FILE:
case REB_URL:
if (!ANY_BINSTR(value) || IS_BINARY(value))
fail (Error_Invalid_Arg(value));
break;
}

VAL_SET_TYPE_BITS(value, kind);
*D_OUT = *value;
return R_OUT;
}


//
// aliases?: native [
//
// {Return whether or not the underlying data of one value aliases another}
//
// value1 [any-series!]
// value2 [any-series!]
// ]
//
REBNATIVE(aliases_q)
{
PARAM(1, value1);
PARAM(2, value2);

if (VAL_SERIES(ARG(value1)) == VAL_SERIES(ARG(value2)))
return R_TRUE;

return R_FALSE;
}


//
// set?: native [
//
Expand Down

0 comments on commit 296c3f5

Please sign in to comment.