Skip to content

Commit

Permalink
Start the PL/PIR, PL/PIRU bifurcation
Browse files Browse the repository at this point in the history
Some odd test failures still exist for returning values from PIRU.
  • Loading branch information
leto committed May 21, 2010
1 parent cd79f29 commit 4b4f9fc
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 10 deletions.
35 changes: 29 additions & 6 deletions plparrot.c
Expand Up @@ -80,7 +80,7 @@ typedef struct plparrot_call_data
MemoryContext tmp_cxt;
} plparrot_call_data;

Parrot_Interp interp, untrusted_interp;
Parrot_Interp interp, untrusted_interp, trusted_interp;

/* Helper functions */
Parrot_String create_string(const char *name);
Expand Down Expand Up @@ -112,20 +112,20 @@ _PG_init(void)
imcc_initialize(untrusted_interp);

/* Must use the first created interp as the parent of subsequently created interps */
interp = Parrot_new(untrusted_interp);
imcc_initialize(interp);
trusted_interp = Parrot_new(untrusted_interp);
imcc_initialize(trusted_interp);

//Parrot_set_trace(interp, PARROT_ALL_TRACE_FLAGS);

if (!interp) {
if (!trusted_interp) {
elog(ERROR,"Could not create a trusted Parrot interpreter!\n");
return;
}
if (!untrusted_interp) {
elog(ERROR,"Could not create an untrusted Parrot interpreter!\n");
return;
}

interp = trusted_interp;
plparrot_secure(interp);

inited = true;
Expand All @@ -139,17 +139,31 @@ _PG_init(void)
void
_PG_fini(void)
{
Parrot_destroy(interp);
Parrot_destroy(trusted_interp);
Parrot_destroy(untrusted_interp);

inited = false;
}

Datum plparrot_call_handler(PG_FUNCTION_ARGS);
Datum plparrotu_call_handler(PG_FUNCTION_ARGS);
static Datum plparrot_func_handler(PG_FUNCTION_ARGS);
static Datum plparrotu_func_handler(PG_FUNCTION_ARGS);

/* The PostgreSQL function+trigger managers call this function for execution
of PL/Parrot procedures. */

PG_FUNCTION_INFO_V1(plparrot_call_handler);
PG_FUNCTION_INFO_V1(plparrotu_call_handler);

static Datum
plparrotu_func_handler(PG_FUNCTION_ARGS)
{
interp = untrusted_interp;
return plparrot_func_handler(fcinfo);
interp = trusted_interp;
}

/*
* The PostgreSQL function+trigger managers call this function for execution of
* PL/Parrot procedures.
Expand Down Expand Up @@ -291,6 +305,15 @@ plparrot_push_pgdatatype_pmc(Parrot_PMC func_args, FunctionCallInfo fcinfo, int
elog(ERROR,"PL/Parrot does not know how to convert the %u element type", element_type);
}
}

Datum
plparrotu_call_handler(PG_FUNCTION_ARGS)
{
interp = untrusted_interp;
plparrot_call_handler(fcinfo);
interp = trusted_interp;
}

Datum
plparrot_call_handler(PG_FUNCTION_ARGS)
{
Expand Down
10 changes: 10 additions & 0 deletions plparrot.sql.in
@@ -1,9 +1,19 @@
-- handler function
CREATE FUNCTION plparrotu_call_handler ()
RETURNS language_handler AS 'MODULE_PATHNAME' LANGUAGE C;

-- handler function
CREATE FUNCTION plparrot_call_handler ()
RETURNS language_handler AS 'MODULE_PATHNAME' LANGUAGE C;

-- language
CREATE LANGUAGE plparrot HANDLER plparrot_call_handler;

-- language
CREATE LANGUAGE plparrotu HANDLER plparrotu_call_handler;

-- plpir is an alias for plparrot
CREATE LANGUAGE plpir HANDLER plparrot_call_handler;

-- plpiru is an alias for plparrotu
CREATE LANGUAGE plpiru HANDLER plparrotu_call_handler;
34 changes: 30 additions & 4 deletions t/sql/test.sql
Expand Up @@ -15,7 +15,7 @@ BEGIN;
\i plparrot.sql

-- Plan the tests.
SELECT plan(26);
SELECT plan(29);

CREATE OR REPLACE FUNCTION create_plparrot()
RETURNS BOOLEAN
Expand Down Expand Up @@ -43,6 +43,16 @@ CREATE FUNCTION test_plpir(int) RETURNS int LANGUAGE plpir AS $$
.return(1)
$$;

CREATE FUNCTION test_plpiru(int) RETURNS int LANGUAGE plpiru AS $$
.param int x
.return(42)
$$;

CREATE FUNCTION test_plparrotu(int) RETURNS int LANGUAGE plparrotu AS $$
.param int x
.return(42)
$$;

CREATE FUNCTION test_immutable(int) RETURNS int AS $$
.param int x
.return(1)
Expand Down Expand Up @@ -143,19 +153,28 @@ CREATE FUNCTION test_time_out(time) RETURNS time AS $$
$$ LANGUAGE plparrot;

CREATE FUNCTION test_open() RETURNS int AS $$
$P0 = open ".", 'r'
$P0 = open "zanzibar", 'r'
.return($P0)
$$ LANGUAGE plparrot;

CREATE FUNCTION test_open_plparrotu() RETURNS int AS $$
push_eh got_error
$P0 = open "somejunkthatdoesntexist", 'r'
pop_eh
.return(42)
got_error:
.return(-1)
$$ LANGUAGE plparrotu;

CREATE FUNCTION test_filehandle_open() RETURNS int AS $$
$P1 = new 'FileHandle'
$P0 = $P1.'open'(".", 'r')
$P0 = $P1.'open'("stuff", 'r')
.return($P0)
$$ LANGUAGE plparrot;

CREATE FUNCTION test_file_open() RETURNS int AS $$
$P1 = new 'File'
$P0 = $P1.'open'(".", 'r')
$P0 = $P1.'open'("blah", 'r')
.return($P0)
$$ LANGUAGE plparrot;

Expand All @@ -169,6 +188,9 @@ $$ LANGUAGE plparrot;

select is(test_open(), 42, 'open opcode is mocked');
select is(test_filehandle_open(), 42, 'FileHandle.open is mocked');

select isnt(test_open_plparrotu(), 42, 'open opcode is not mocked in plperlu');

select is(test_file_open(), 42, 'File.open is mocked');

select is(test_text_in('cheese'), 'cheese', 'We can pass a text in');
Expand All @@ -187,6 +209,10 @@ select is(test_int_out(1),42,'We can return an int');

select is(test_plpir(1),1,'plpir is an alias for plparrot');

-- Why does this fail? What happens to the return value?
select is(test_plpiru(1),42,'plpiru can return values');
select is(test_plparrotu(1),42,'plparrotu can return values');

select is(test_immutable(42),1,'Immutable works');
select is(test_strict(NULL), NULL, 'Strict works');

Expand Down

0 comments on commit 4b4f9fc

Please sign in to comment.