Name
SPVM::Document::Resource - How to write the resource class
Resource
A resource is a native class that contains a set of sources and headers of native language such as the C language or C++
.
A resource doesn't has the native class file such as Foo.c
. It has a config file such as Foo.config
. Header files are placed at Foo.native/include
. Source filies are placed at Foo.native/src
.
Let's see the files of Resource::Zlib as an example.
SPVM/Resource/Zlib/V1_2_11.config
use strict;
use warnings;
use SPVM::Builder::Config;
my $config = SPVM::Builder::Config->new_c99(file => __FILE__);
# C souce files
my @source_files = qw(
adler32.c
compress.c
crc32.c
deflate.c
gzclose.c
gzlib.c
gzread.c
gzwrite.c
infback.c
inffast.c
inflate.c
inftrees.c
trees.c
uncompr.c
zutil.c
);
$config->add_source_file(@source_files);
my @ccflags = '-D_LARGEFILE64_SOURCE';
$config->add_ccflag(@ccflags);
$config;
The list of the files in include
directory
The header files of zlib
. These files are the header files extracted from src
directory.
crc32.h
deflate.h
gzguts.h
inffast.h
inffixed.h
inflate.h
inftrees.h
trees.h
zconf.h
zlib.h
zutil.h
The list of the files in src
directory
The source files of zlib
.
adler32.c
amiga
ChangeLog
CMakeLists.txt
compress.c
configure
contrib
crc32.c
deflate.c
doc
examples
FAQ
gzclose.c
gzlib.c
gzread.c
gzwrite.c
INDEX
infback.c
inffast.c
inflate.c
inftrees.c
Makefile
Makefile.in
make_vms.com
msdos
nintendods
old
os400
qnx
README
test
treebuild.xml
trees.c
uncompr.c
watcom
win32
zconf.h.cmakein
zconf.h.in
zlib2ansi
zlib.3
zlib.3.pdf
zlib.map
zlib.pc.cmakein
zlib.pc.in
zutil.c
Using Resource
The method "use_resource" in SPVM::Builder::Config loads a resource. MyZlib
is a native class to use Resource::Zlib.
lib/SPVM/MyZlib.config
use strict;
use warnings;
my $config = SPVM::Builder::Config->new_c99(file => __FILE__);
$config->use_resource('Resource::Zlib');
$config;
lib/SPVM/MyZlib.spvm
Define a native method test_gzopen_gzread
.
class MyZlib {
native static method test_gzopen_gzread : void ($file : string);
}
lib/SPVM/MyZlib.c
zlib.h
can be included because Resource::Zlib is used.
#include "spvm_native.h"
#include <zlib.h>
int32_t SPVM__MyZlib__test_gzopen_gzread(SPVM_ENV* env, SPVM_VALUE* stack) {
(void)env;
void* sp_file = stack[0].oval;
const char* file = env->get_chars(env, stack, sp_file);
z_stream z;
gzFile gz_fh = gzopen(file, "rb");
if (gz_fh == NULL){
return env->die(env, stack, "Can't open file \"%s\"\n", __func__, file, __LINE__);
}
char buffer[256] = {0};
int32_t cnt;
while((cnt = gzread(gz_fh, buffer, sizeof(buffer))) > 0){
}
printf("%s", buffer);
return 0;
}
myzlib.pl
A Perl script to call test_gzopen_gzread
method of MyZlib
class.
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
BEGIN { $ENV{SPVM_BUILD_DIR} = "$FindBin::Bin/.spvm_build"; }
use SPVM 'MyZlib';
my $gz_file = "$FindBin::Bin/minitest.txt.gz";
SPVM::MyZlib->test_gzopen_gzread($gz_file);
Creating Resource Distribution
spvmdist command with --resource
option create a resource distribution.
# C language resource
spvmdist --resource Resource::Foo
# C++ resource
spvmdist --resource --native c++ Resource::Foo
Resource Modules
Copyright & License
Copyright (c) 2023 Yuki Kimoto
MIT License