From 63d7ef8d06c8d7880fcc8ac50a96c4f4754b6f1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Grange?= Date: Sun, 19 Dec 2010 16:49:33 +0100 Subject: [PATCH] Add Parrot_api_pmc_find_method() function and improve t/src/embed/pmc.t to test it --- include/parrot/api.h | 11 +++++++++ src/embed/pmc.c | 12 ++++++++- t/src/embed/pmc.t | 59 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/include/parrot/api.h b/include/parrot/api.h index d64e128513..13fe6f934d 100644 --- a/include/parrot/api.h +++ b/include/parrot/api.h @@ -427,6 +427,15 @@ Parrot_Int Parrot_api_pmc_deserialize_bytes( __attribute__nonnull__(4) FUNC_MODIFIES(* pmc); +PARROT_API +Parrot_Int Parrot_api_pmc_find_method( + Parrot_PMC interp_pmc, + Parrot_PMC object, + Parrot_String name, + ARGOUT(Parrot_PMC *method)) + __attribute__nonnull__(4) + FUNC_MODIFIES(*method); + PARROT_API Parrot_Int Parrot_api_pmc_get_class( Parrot_PMC interp_pmc, @@ -569,6 +578,8 @@ Parrot_Int Parrot_api_pmc_wrap_string_array( __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ PARROT_ASSERT_ARG(fpmc) \ , PARROT_ASSERT_ARG(pmc)) +#define ASSERT_ARGS_Parrot_api_pmc_find_method __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ + PARROT_ASSERT_ARG(method)) #define ASSERT_ARGS_Parrot_api_pmc_get_class __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ PARROT_ASSERT_ARG(class)) #define ASSERT_ARGS_Parrot_api_pmc_get_float __attribute__unused__ int _ASSERT_ARGS_CHECK = (\ diff --git a/src/embed/pmc.c b/src/embed/pmc.c index b245424077..6abcfa7b96 100644 --- a/src/embed/pmc.c +++ b/src/embed/pmc.c @@ -204,7 +204,7 @@ PARROT_API Parrot_Int Parrot_api_pmc_invoke(Parrot_PMC interp_pmc, Parrot_PMC sub, Parrot_PMC signature) { - EMBED_API_CALLIN(interp_pmc, interp); + EMBED_API_CALLIN(interp_pmc, interp) PMC * const old_call_obj = Parrot_pcc_get_signature(interp, CURRENT_CONTEXT(interp)); Parrot_pcc_invoke_from_sig_object(interp, sub, signature); @@ -243,3 +243,13 @@ Parrot_api_pmc_get_class(Parrot_PMC interp_pmc, Parrot_PMC key, *class = Parrot_oo_get_class(interp, key); EMBED_API_CALLOUT(interp_pmc, interp); } + + +PARROT_API +Parrot_Int +Parrot_api_pmc_find_method(Parrot_PMC interp_pmc, Parrot_PMC object, Parrot_String name, ARGOUT(Parrot_PMC *method)) +{ + EMBED_API_CALLIN(interp_pmc, interp); + *method = VTABLE_find_method(interp, object, name); + EMBED_API_CALLOUT(interp_pmc, interp); +} diff --git a/t/src/embed/pmc.t b/t/src/embed/pmc.t index 641be8a857..5023b513a5 100644 --- a/t/src/embed/pmc.t +++ b/t/src/embed/pmc.t @@ -24,7 +24,7 @@ Tests PMC API support. =cut -plan tests => 4; +plan tests => 5; c_output_is( <<'CODE', <<'OUTPUT', "get/set_keyed_int" ); @@ -206,6 +206,63 @@ This is a string! 3.1415 OUTPUT +c_output_is( <<'CODE', <<'OUTPUT', "PMC find_method" ); + +#include +#include + +int main(int argc, char* argv[]) +{ + Parrot_Init_Args *initargs = NULL; + Parrot_PMC interpmc = NULL; + Parrot_PMC p_pmc = NULL, p_method = NULL, p_signature = NULL, p_classname = NULL; + Parrot_PMC p_call_class = NULL, p_toreplace = NULL, p_replacestring = NULL; + Parrot_String s_teststr = NULL, s_outstr = NULL, s_method = NULL, s_classname = NULL; + Parrot_String s_signstring = NULL, s_toreplace = NULL, s_replacestring = NULL; + Parrot_PMC p_keyedstr = NULL; + Parrot_PMC p_idx = NULL; + char * c_out = NULL; + + GET_INIT_STRUCT(initargs); + Parrot_api_make_interpreter(NULL, 0, initargs, &interpmc); + + Parrot_api_string_import_ascii(interpmc, "I love Microsoft!", &s_teststr); + Parrot_api_pmc_box_string(interpmc, s_teststr, &p_pmc); + + Parrot_api_string_import_ascii(interpmc, "replace", &s_teststr); + Parrot_api_pmc_find_method(interpmc, p_pmc, s_teststr, &p_method); + if(p_method != NULL) { + Parrot_api_string_import_ascii(interpmc, "CallContext", &s_classname); + Parrot_api_pmc_box_string(interpmc, s_classname, &p_classname); + Parrot_api_pmc_get_class(interpmc, p_classname, &p_call_class); + Parrot_api_pmc_new_from_class(interpmc, p_call_class, NULL, &p_signature); + + Parrot_api_string_import_ascii(interpmc, "PiSS->", &s_signstring); + Parrot_api_pmc_set_string(interpmc, p_signature, s_signstring); + Parrot_api_pmc_set_keyed_int(interpmc, p_signature, 0, p_pmc); + + Parrot_api_string_import_ascii(interpmc, "Microsoft", &s_toreplace); + Parrot_api_pmc_box_string(interpmc, s_toreplace, &p_toreplace); + Parrot_api_pmc_set_keyed_int(interpmc, p_signature, 1, p_toreplace); + + Parrot_api_string_import_ascii(interpmc, "the Open Source community", &s_replacestring); + Parrot_api_pmc_box_string(interpmc, s_replacestring, &p_replacestring); + Parrot_api_pmc_set_keyed_int(interpmc, p_signature, 2, p_replacestring); + + Parrot_api_pmc_invoke(interpmc, p_method, p_signature); + Parrot_api_pmc_get_string(interpmc, p_pmc, &s_outstr); + Parrot_api_string_export_ascii(interpmc, s_outstr, &c_out); + printf("%s\n", c_out); + } + else printf("error\n"); + + return 0; +} + +CODE +I love the Open Source community! +OUTPUT + # Local Variables: # mode: cperl # cperl-indent-level: 4