From e30e103c3fe9b1bad64f78be8d61bb13d3007a3e Mon Sep 17 00:00:00 2001 From: Matt Rajca Date: Fri, 17 Dec 2010 19:56:21 -0600 Subject: [PATCH 1/2] New API functions to perform string-keyed lookup and set --- include/parrot/api.h | 21 +++++++++++++++++++++ src/embed/pmc.c | 18 ++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/include/parrot/api.h b/include/parrot/api.h index 595516957c..559f9e537a 100644 --- a/include/parrot/api.h +++ b/include/parrot/api.h @@ -448,6 +448,15 @@ Parrot_Int Parrot_api_pmc_get_integer( __attribute__nonnull__(3) FUNC_MODIFIES(* value); +PARROT_API +Parrot_Int Parrot_api_pmc_get_keyed_string( + Parrot_PMC interp_pmc, + Parrot_PMC pmc, + Parrot_String key, + ARGOUT(Parrot_PMC * value)) + __attribute__nonnull__(4) + FUNC_MODIFIES(* value); + PARROT_API Parrot_Int Parrot_api_pmc_get_string( Parrot_PMC interp_pmc, @@ -475,6 +484,13 @@ Parrot_Int Parrot_api_pmc_set_integer( Parrot_PMC pmc, Parrot_Int value); +PARROT_API +Parrot_Int Parrot_api_pmc_set_keyed_string( + Parrot_PMC interp_pmc, + Parrot_PMC pmc, + Parrot_String key, + Parrot_PMC value); + PARROT_API Parrot_Int Parrot_api_pmc_set_string( Parrot_PMC interp_pmc, @@ -495,12 +511,17 @@ Parrot_Int Parrot_api_pmc_set_string( PARROT_ASSERT_ARG(value)) #define ASSERT_ARGS_Parrot_api_pmc_get_integer __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ PARROT_ASSERT_ARG(value)) +#define ASSERT_ARGS_Parrot_api_pmc_get_keyed_string \ + __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ + PARROT_ASSERT_ARG(value)) #define ASSERT_ARGS_Parrot_api_pmc_get_string __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ PARROT_ASSERT_ARG(str)) #define ASSERT_ARGS_Parrot_api_pmc_null __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ PARROT_ASSERT_ARG(pmctonull)) #define ASSERT_ARGS_Parrot_api_pmc_set_float __attribute__unused__ int _ASSERT_ARGS_CHECK = (0) #define ASSERT_ARGS_Parrot_api_pmc_set_integer __attribute__unused__ int _ASSERT_ARGS_CHECK = (0) +#define ASSERT_ARGS_Parrot_api_pmc_set_keyed_string \ + __attribute__unused__ int _ASSERT_ARGS_CHECK = (0) #define ASSERT_ARGS_Parrot_api_pmc_set_string __attribute__unused__ int _ASSERT_ARGS_CHECK = (0) /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */ /* HEADERIZER END: src/embed/pmc.c */ diff --git a/src/embed/pmc.c b/src/embed/pmc.c index 473a8392c1..5a4ca1bc00 100644 --- a/src/embed/pmc.c +++ b/src/embed/pmc.c @@ -96,6 +96,24 @@ Parrot_api_pmc_set_float(Parrot_PMC interp_pmc, Parrot_PMC pmc, EMBED_API_CALLOUT(interp_pmc, interp) } +PARROT_API +Parrot_Int +Parrot_api_pmc_get_keyed_string(Parrot_PMC interp_pmc, Parrot_PMC pmc, Parrot_String key, ARGOUT(Parrot_PMC * value)) +{ + EMBED_API_CALLIN(interp_pmc, interp) + *value = VTABLE_get_pmc_keyed_str(interp, pmc, key); + EMBED_API_CALLOUT(interp_pmc, interp) +} + +PARROT_API +Parrot_Int +Parrot_api_pmc_set_keyed_string(Parrot_PMC interp_pmc, Parrot_PMC pmc, Parrot_String key, Parrot_PMC value) +{ + EMBED_API_CALLIN(interp_pmc, interp) + VTABLE_set_pmc_keyed_str(interp, pmc, key, value); + EMBED_API_CALLOUT(interp_pmc, interp) +} + PARROT_API Parrot_Int Parrot_api_add_exception_handler(Parrot_PMC interp_pmc, Parrot_PMC handler) From 5b5206648f4055cc852cbfd7e6fea8ca81c5bc6c Mon Sep 17 00:00:00 2001 From: Matt Rajca Date: Fri, 17 Dec 2010 19:57:12 -0600 Subject: [PATCH 2/2] Added test --- t/src/embed/pmc.t | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 t/src/embed/pmc.t diff --git a/t/src/embed/pmc.t b/t/src/embed/pmc.t new file mode 100644 index 0000000000..e008f19c0d --- /dev/null +++ b/t/src/embed/pmc.t @@ -0,0 +1,82 @@ +#!perl +# Copyright (C) 2010, Parrot Foundation. + +use strict; +use warnings; + +use lib qw(. lib ../lib ../../lib ); + +use Test::More; +use Parrot::Test; +use Parrot::Config; + +=head1 NAME + +t/src/embed/pmc.t - Parrot API PMC tests + +=head1 SYNPOSIS + + % prove t/src/embed/pmc.t + +=head1 DESCRIPTION + +Tests PMC API support. + +=cut + +plan tests => 1; + +c_output_is( <<'CODE', <<'OUTPUT', "Tests get_keyed_string and set_keyed_string" ); + +#include +#include +#include +#include + +#define TEST_STR "The quick brown fox jumps over the lazy dog" + +int main(int argc, char* argv[]) +{ + Parrot_Interp interp = Parrot_new(NULL); + + Parrot_PMC interp_pmc = Parrot_pmc_new(interp, enum_class_ParrotInterpreter); + Parrot_PMC hash_pmc = Parrot_pmc_new(interp, enum_class_Hash); + Parrot_PMC str_pmc = Parrot_pmc_new(interp, enum_class_String); + + Parrot_String test_str = Parrot_str_new(interp, TEST_STR, 0); + Parrot_api_pmc_set_string(interp_pmc, str_pmc, test_str); + + Parrot_String idx_str = Parrot_str_new(interp, "name", 0); + Parrot_api_pmc_set_keyed_string(interp_pmc, hash_pmc, idx_str, str_pmc); + + Parrot_PMC str_pmc_out = NULL; + Parrot_String str_out = NULL; + + Parrot_api_pmc_get_keyed_string(interp_pmc, hash_pmc, idx_str, &str_pmc_out); + Parrot_api_pmc_get_string(interp_pmc, str_pmc_out, &str_out); + + if (strcmp(Parrot_str_to_cstring(interp, str_out), TEST_STR) != 0) { + printf("Failed accessing a keyed Hash PMC\n"); + return EXIT_FAILURE; + } + + printf("ok 1\n"); + + Parrot_pmc_destroy(interp, interp_pmc); + Parrot_pmc_destroy(interp, hash_pmc); + Parrot_pmc_destroy(interp, str_pmc); + + return 0; +} + +CODE +ok 1 +OUTPUT + +# Local Variables: +# mode: cperl +# cperl-indent-level: 4 +# fill-column: 100 +# End: +# vim: expandtab shiftwidth=4: +