Skip to content

Commit

Permalink
Merge branch 'embed_api2' of github.com:parrot/parrot into embed_api2
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiteknight committed Dec 13, 2010
2 parents c31e7a5 + 5f5c181 commit 8b915b2
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/gen/makefiles/root.in
Expand Up @@ -1790,6 +1790,7 @@ src/pbc_merge$(O) : \
$(INC_DIR)/oplib/ops.h \
$(INC_DIR)/oplib/core_ops.h \
$(INC_DIR)/runcore_api.h \
$(INC_DIR)/embed.h \
$(PARROT_H_HEADERS)

src/io/filehandle$(O) : $(PARROT_H_HEADERS) include/pmc/pmc_filehandle.h \
Expand Down
36 changes: 36 additions & 0 deletions include/parrot/api.h
Expand Up @@ -10,6 +10,7 @@
#define PARROT_API_H_GUARD

#include <stdlib.h>
#include <wchar.h>
#include "parrot/compiler.h"
#include "parrot/config.h"
#include "parrot/core_types.h"
Expand Down Expand Up @@ -319,12 +320,27 @@ Parrot_Int Parrot_api_string_export_ascii(
__attribute__nonnull__(3)
FUNC_MODIFIES(* strout);

PARROT_API
Parrot_Int Parrot_api_string_export_wchar(
Parrot_PMC interp_pmc,
ARGIN(Parrot_String string),
ARGOUT(wchar_t ** strout))
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(* strout);

PARROT_API
Parrot_Int Parrot_api_string_free_exported_ascii(
Parrot_PMC interp_pmc,
ARGIN(char * const str))
__attribute__nonnull__(2);

PARROT_API
Parrot_Int Parrot_api_string_free_exported_wchar(
Parrot_PMC interp_pmc,
ARGIN(wchar_t * const str))
__attribute__nonnull__(2);

PARROT_API
Parrot_Int Parrot_api_string_import_ascii(
Parrot_PMC interp_pmc,
Expand All @@ -344,13 +360,29 @@ Parrot_Int Parrot_api_string_import_binary(
__attribute__nonnull__(4)
FUNC_MODIFIES(*out);

PARROT_API
Parrot_Int Parrot_api_string_import_wchar(
Parrot_PMC interp_pmc,
ARGIN(wchar_t * str),
ARGOUT(Parrot_String * out))
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(* out);

#define ASSERT_ARGS_Parrot_api_string_export_ascii \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(string) \
, PARROT_ASSERT_ARG(strout))
#define ASSERT_ARGS_Parrot_api_string_export_wchar \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(string) \
, PARROT_ASSERT_ARG(strout))
#define ASSERT_ARGS_Parrot_api_string_free_exported_ascii \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(str))
#define ASSERT_ARGS_Parrot_api_string_free_exported_wchar \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(str))
#define ASSERT_ARGS_Parrot_api_string_import_ascii \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(str) \
Expand All @@ -359,6 +391,10 @@ Parrot_Int Parrot_api_string_import_binary(
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(bytes) \
, PARROT_ASSERT_ARG(out))
#define ASSERT_ARGS_Parrot_api_string_import_wchar \
__attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(str) \
, PARROT_ASSERT_ARG(out))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: src/embed/strings.c */

Expand Down
56 changes: 55 additions & 1 deletion src/embed/strings.c
Expand Up @@ -27,6 +27,40 @@ Parrot_api_string_free_exported_ascii(Parrot_PMC interp_pmc, ARGIN(char * const
EMBED_API_CALLOUT(interp_pmc, interp);
}

PARROT_API
Parrot_Int
Parrot_api_string_export_wchar(Parrot_PMC interp_pmc, ARGIN(Parrot_String string), ARGOUT(wchar_t ** strout))
{
char *cstr;
size_t len;
wchar_t *wstrout;

EMBED_API_CALLIN(interp_pmc, interp);
if(!STRING_IS_NULL(string)) {
cstr = Parrot_str_to_cstring(interp, string);
len = strlen(cstr);

wstrout = (wchar_t *) malloc (sizeof(wchar_t) * len + 1);
mbstowcs(wstrout, cstr, len);
wstrout[len] = L'\0';

*strout = wstrout;
}
else
*strout = NULL;
EMBED_API_CALLOUT(interp_pmc, interp);
}

PARROT_API
Parrot_Int
Parrot_api_string_free_exported_wchar(Parrot_PMC interp_pmc, ARGIN(wchar_t * const str))
{
EMBED_API_CALLIN(interp_pmc, interp);
if(str != NULL)
free(str);
EMBED_API_CALLOUT(interp_pmc, interp);
}

PARROT_API
Parrot_Int
Parrot_api_string_import_ascii(Parrot_PMC interp_pmc, ARGIN(const char * str), ARGOUT(Parrot_String * out))
Expand All @@ -37,6 +71,27 @@ Parrot_api_string_import_ascii(Parrot_PMC interp_pmc, ARGIN(const char * str), A
EMBED_API_CALLOUT(interp_pmc, interp);
}

PARROT_API
Parrot_Int
Parrot_api_string_import_wchar(Parrot_PMC interp_pmc, ARGIN(wchar_t * str), ARGOUT(Parrot_String * out))
{
char *cstr;
size_t len;

EMBED_API_CALLIN(interp_pmc, interp);

len = wcslen(str);

cstr = (char *) malloc (sizeof(char) * len + 1);
wcstombs(cstr, str, len);
cstr[len] = '\0';

*out = Parrot_str_new(interp, cstr, 0);
free(cstr);

EMBED_API_CALLOUT(interp_pmc, interp);
}

PARROT_API
Parrot_Int
Parrot_api_string_import_binary(Parrot_PMC interp_pmc, ARGIN(const unsigned char *bytes), Parrot_Int length, ARGOUT(Parrot_String *out))
Expand All @@ -46,4 +101,3 @@ Parrot_api_string_import_binary(Parrot_PMC interp_pmc, ARGIN(const unsigned char
EMBED_API_CALLOUT(interp_pmc, interp);
}

/* TODO: wchar_t variants */
1 change: 1 addition & 0 deletions src/pbc_merge.c
Expand Up @@ -42,6 +42,7 @@ segments from the input PBC files.
#include "parrot/oplib/ops.h"
#include "parrot/oplib/core_ops.h"
#include "pmc/pmc_sub.h"
#include "parrot/embed.h"

/* This struct describes an input file. */
typedef struct pbc_merge_input {
Expand Down
79 changes: 79 additions & 0 deletions t/src/embed/strings.t
@@ -0,0 +1,79 @@
#!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/strings.t - Parrot API string operations
=head1 SYNPOSIS
% prove t/src/embed/strings.t
=head1 DESCRIPTION
Tests string API support.
=cut

# generic tests

plan tests => 1;

c_output_is( <<'CODE', <<'OUTPUT', "wchar import / export" );
#include <parrot/parrot.h>
#include <parrot/embed.h>
#include <parrot/api.h>
#include <stdio.h>
#define TEST_STR "The quick brown fox jumps over the lazy dog"
#define TEST_STR_L L"The quick brown fox jumps over the lazy dog"
int main(int argc, char* argv[])
{
Parrot_Interp interp = Parrot_new(NULL);
Parrot_PMC pmc = Parrot_pmc_new(interp, enum_class_ParrotInterpreter);
Parrot_String str = NULL;
Parrot_api_string_import_wchar(pmc, TEST_STR_L, &str);
if (strcmp(Parrot_str_to_cstring(interp, str), TEST_STR) != 0) {
printf("Failed to import a wchar string into a Parrot_string");
return EXIT_FAILURE;
}
printf("ok 1\n");
wchar_t *wcout;
Parrot_api_string_export_wchar(pmc, str, &wcout);
Parrot_pmc_destroy(interp, pmc);
if (wcscmp(wcout, TEST_STR_L) != 0) {
printf("Failed to export a wchar string from a Parrot_string");
return EXIT_FAILURE;
}
printf("ok 2\n");
return EXIT_SUCCESS;
}
CODE
ok 1
ok 2
OUTPUT

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4:

0 comments on commit 8b915b2

Please sign in to comment.