Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Rename Niecza::Interoperability to Niecza.
Make Niecza a full Module::Build using module with an XS part.
  • Loading branch information
pmurias committed Feb 24, 2012
1 parent 4927ce0 commit 02fe910
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 171 deletions.
2 changes: 1 addition & 1 deletion lib/CodeGen.cs
Expand Up @@ -3694,7 +3694,7 @@ public class DowncallReceiver : CallReceiver {
}
public static object unit_use_perl5_module(object[] args) {
string name = (string)args[2];
Variable code = Kernel.BoxAnyMO<string>("Niecza::Interoperability::use_module('"+name+"')", Kernel.StrMO);
Variable code = Kernel.BoxAnyMO<string>("Niecza::Helpers::use_module('"+name+"')", Kernel.StrMO);
string sub = Kernel.UnboxAny<string>(Builtins.eval_perl5(code).Fetch());
return sub;
}
Expand Down
16 changes: 13 additions & 3 deletions lib/Perl5Interpreter.cs
Expand Up @@ -6,7 +6,7 @@

public class Perl5Interpreter : IForeignInterpreter {
[DllImport("p5embed", EntryPoint="p5embed_initialize")]
public static extern void Initialize(string p5lib);
public static extern void Initialize(string path1,string path2);

[DllImport("p5embed", EntryPoint="p5embed_dispose")]
public static extern void Dispose();
Expand Down Expand Up @@ -86,8 +86,18 @@ int argument_n

public Perl5Interpreter() {
string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
string p5lib = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(location)),"perl5");
Initialize(p5lib);

string[] paths = new string[] {"perl5/Niecza/blib/lib","perl5/Niecza/blib/arch"};

for (int i=0;i<2;i++) {
// Try to construct the path in a platform portable manner
string p5lib = Path.GetDirectoryName(Path.GetDirectoryName(location));
foreach (string part in paths[i].Split('/')) {
p5lib = Path.Combine(p5lib,part);
}
paths[i] = p5lib;
}
Initialize(paths[0],paths[1]);
}
~Perl5Interpreter() {
Dispose();
Expand Down
12 changes: 8 additions & 4 deletions lib/p5embed.c
Expand Up @@ -17,7 +17,7 @@ xs_init(pTHX)
}

static PerlInterpreter *my_perl;
void p5embed_initialize(char* p5lib)
void p5embed_initialize(char* path1,char* path2)
{
int argc = 1;
char *argv0[] = { "perl", NULL, 0 };
Expand All @@ -29,9 +29,13 @@ void p5embed_initialize(char* p5lib)
perl_parse(my_perl, xs_init, 3, embedding, NULL);
PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
AV* INC = get_av("INC",0);
SV* p5lib_sv = newSVpv(p5lib,0);
av_push(INC,p5lib_sv);
eval_pv("use Niecza::Interoperability",TRUE);
char* path[2] = {path1,path2};
int i;
for (i=0;i<2;i++) {
SV* p5lib_sv = newSVpv(path[i],0);
av_push(INC,p5lib_sv);
}
eval_pv("use Niecza",TRUE);
}

SV* p5embed_eval(char* code) {
Expand Down
6 changes: 3 additions & 3 deletions perl5/Niecza/MANIFEST
@@ -1,14 +1,14 @@
Build.PL
Changes
ignore.txt
lib/Niecza.pm
lib/Niecza.xs
lib/Niecza/Helpers.pm
lib/typemap
MANIFEST This list of files
ppport.h
README
t/00-load.t
t/01-object.t
t/02-feature.t
t/boilerplate.t
t/manifest.t
t/pod-coverage.t
t/pod.t
65 changes: 6 additions & 59 deletions perl5/Niecza/lib/Niecza.pm
Expand Up @@ -5,7 +5,7 @@ use strict;

=head1 NAME
Niecza - The great new Niecza!
Niecza - The perl5-niecza interoperability support;

=head1 VERSION
Expand All @@ -18,73 +18,20 @@ our $VERSION = '0.01';
require XSLoader;
XSLoader::load('Niecza', $VERSION);

=head1 SYNOPSIS
Quick summary of what the module does.
Perhaps a little code snippet.
use Niecza;
my $foo = Niecza->new();
...
=head1 EXPORT
A list of functions that can be exported. You can delete this section
if you don't export anything, such as for a purely object-oriented module.
=head1 FUNCTIONS
=head2 new
Creates a new Niecza object. Takes the following optional parameters:
=over 4
=item value
use Niecza::Helpers;

If you pass a single numeric value, it will be stored in the 'value' slot
of the object hash.
=item key/value pair
A generic input method which takes an unlimited number of key/value pairs
and stores them in the object hash. Performs no validation.
=back
=cut

#sub new {
# Defined in the XS code
#}

=head2 increment
An object method which increments the 'value' slot of the the object hash,
if it exists. Called like this:
my $obj = Niecza->new(5);
$obj->increment(); # now equal to 6
=cut
=head1 SYNOPSIS
#sub function2 {
# Defined in the XS code
#}
use v6;
eval(:lang<perl5>,"...");

=head1 AUTHOR
Paweł Murias, C<< <pawelmurias@gmail.com> >>

=head1 BUGS
Please report any bugs or feature requests to
C<bug-niecza@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Niecza>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
Please report any bugs or feature requests on the #perl6 on freenode.org IRC channel or using the github bugtracker for niecza.

=head1 ACKNOWLEDGEMENTS
Expand Down
57 changes: 0 additions & 57 deletions perl5/Niecza/lib/Niecza.xs
Expand Up @@ -4,63 +4,6 @@

#include "ppport.h"

typedef SV * Niecza;

MODULE = Niecza PACKAGE = Niecza

Niecza
new(...)
INIT:
char *classname;
/* get the class name if called as an object method */
if ( sv_isobject(ST(0)) ) {
classname = HvNAME(SvSTASH(SvRV(ST(0))));
}
else {
classname = (char *)SvPV_nolen(ST(0));
}

CODE:
/* This is a standard hash-based object */
RETVAL = (Niecza)newHV();

/* Single init value */
if ( items == 2 )
hv_store((HV *)RETVAL, "value", 5, newSVsv(ST(1)), 0);
/* name/value pairs */
else if ( (items-1)%2 == 0 ) {
int i;
for ( i=1; i < items; i += 2 ) {
hv_store_ent((HV *)RETVAL, ST(i), newSVsv(ST(i+1)), 0);
}
}
/* odd number of parameters */
else {
Perl_croak(aTHX_
"Usage: Niecza->new()\n"
" or Niecza->new(number)\n"
" or Niecza->new(key => value, ...)\n"
);
}

OUTPUT:
RETVAL

IV
increment(obj)
Niecza obj

INIT:
RETVAL = 0;
if ( items > 1 )
Perl_croak(aTHX_ "Usage: Niecza->increment()");

CODE:
SV **svp;
if ((svp = hv_fetch((HV*)obj, "value", 5, FALSE))) {
RETVAL = SvIV(*svp);
RETVAL++;
hv_store((HV *)obj, "value", 5, newSViv(RETVAL), 0);
}
OUTPUT:
RETVAL
15 changes: 15 additions & 0 deletions perl5/Niecza/lib/Niecza/Helpers.pm
@@ -0,0 +1,15 @@
package Niecza::Helpers;
use Package::Stash;
my $id = 0;
sub use_module {
my ($module) = @_;
my $stash = Package::Stash->new('main');#'Niecza::Stash::Interopability::TMP'.$id++);
eval("package ".$stash->name.";use $module;");
warn $@ if $@;
my @subs = $stash->list_all_symbols('CODE');
for my $symbol (@subs) {
#say "importing sub $symbol from $module";
}
$subs[0];
}
1;
9 changes: 0 additions & 9 deletions perl5/Niecza/t/00-load.t

This file was deleted.

22 changes: 0 additions & 22 deletions perl5/Niecza/t/01-object.t

This file was deleted.

13 changes: 0 additions & 13 deletions perl5/Niecza/t/02-feature.t

This file was deleted.

1 change: 1 addition & 0 deletions perl5/build_interop
Expand Up @@ -17,3 +17,4 @@ chomp($ldopts);

my $cc = "$Config{cc} -m32 -shared";
system("$cc lib/p5embed.c -o $path $ccopts $ldopts");
system("cd perl5/Niecza;perl Build.PL;perl Build");

0 comments on commit 02fe910

Please sign in to comment.