perl XS

perl XS,第1张

概述h2xs -A FloatCalc 然后改写FloatCalc.xs,以下提供3种有别于标准写法的方式。 方式一: #define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"#include <stdio.h>XS(XS_FloatCalc_pow){ dXS

h2xs -A floatCalc

然后改写floatCalc.xs,以下提供3种有别于标准写法的方式。

方式一:

#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"#include <stdio.h>XS(XS_floatCalc_pow){    dXSARGS;    if (items != 2)       croak("usage:floatCalc::pow(x,y)");    {		double	RETVAL;		dXSTARG;		RETVAL = pow(SvNV(ST(0)),SvNV(ST(1)) );		XSprePUSH; 		PUSHn(RETVAL);    }    XSRETURN(1);}XS(boot_floatCalc){    dXSARGS;    const char* file = __file__;	newXS("floatCalc::pow",XS_floatCalc_pow,file);	if (PL_unitcheckav)		call_List(PL_scopestack_ix,PL_unitcheckav);    XSRETURN_YES;}


方式二:

#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"#include <stdio.h>XS(XS_floatCalc_pow){    dXSARGS;    if (items != 2)       croak("usage:floatCalc::pow(x,y)");    {		double result;		sp = mark;		result = pow(SvNV(ST(0)),SvNV(ST(1)) );		xpuSHs(sv_2mortal(newSVnv(result)));    }    XSRETURN(1);}XS(boot_floatCalc){    dXSARGS;    const char* file = __file__;	newXS("floatCalc::pow",PL_unitcheckav);    XSRETURN_YES;}


方式三:

#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"#include <stdio.h>XS(XS_floatCalc_pow){    dSP; 			//得到sp    dAXMARK; 		//得到mark    dITEMS;			//得到items = sp - mark    if (items != 2)       croak("usage:floatCalc::pow(x,y)");    {		double result;		sp = mark;	//回到起始位置		result = pow(SvNV(ST(0)),SvNV(ST(1)) );		xpuSHs(sv_2mortal(newSVnv(result)));    }    PL_stack_sp = sp;	//返回栈帧	return;	}XS(boot_floatCalc){    dXSARGS;    const char* file = __file__;	newXS("floatCalc::pow",PL_unitcheckav);    XSRETURN_YES;}

方式四:

#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"#include <stdio.h>XS(XS_floatCalc_pow){    dXSARGS;    double result;    if (items != 2)    {       croak("usage:floatCalc::pow(x,y)");       XSRETURN_EMPTY;    }    result = pow(SvNV(ST(0)),SvNV(ST(1)) );    XSRETURN_NV(result);}XS(boot_floatCalc){    I32 ax = POPMARK;		//仅需要ax    const char* file = __file__;    newXS("floatCalc::pow",file);    if (PL_unitcheckav)        call_List(PL_scopestack_ix,PL_unitcheckav);    XSRETURN_YES;}

 方式五:

#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"#include <stdio.h>XS(XS_floatCalc_pow){    SV **mark = PL_stack_base + (*PL_markstack_ptr--);    int items = PL_stack_sp - mark;    double	result;    if (items != 2)    {       croak("usage:floatCalc::pow(x,y)");       PL_stack_sp = mark;       return;    }	result = pow(SvNV(mark[1+0]),SvNV(mark[1+1]) );	mark[1+0] = sv_2mortal(newSVnv(result));	PL_stack_sp = mark +1;	return;}XS(boot_floatCalc){    I32 ax = POPMARK;		//仅需要ax    const char* file = __file__;	newXS("floatCalc::pow",PL_unitcheckav);    XSRETURN_YES;}


 

测试:

#!/usr/bin/perluse floatCalc;my $result = pow(2.0,4.0);print "pow(2.0,4.0) = $result\n";my $result = pow(2.0);  # 会有用法提示<>;

结果:

C:\Users\G-SpIDer\floatCalc>test.plpow(2.0,4.0) = 16usage:floatCalc::pow(x,y) at C:\Users\G-SpIDer\Desktop\hello.pl line 6.
总结

以上是内存溢出为你收集整理的perl XS全部内容,希望文章能够帮你解决perl XS所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1277208.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存