Skip to content

Commit

Permalink
bindings/blst.swg: be more consistent with semicolons.
Browse files Browse the repository at this point in the history
  • Loading branch information
dot-asm committed Feb 5, 2021
1 parent f8c2b85 commit f2c6230
Showing 1 changed file with 32 additions and 34 deletions.
66 changes: 32 additions & 34 deletions bindings/blst.swg
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@

// some sorcery to allow assignments as output, e.g.
// hash = blst.encode_to_g1(b"foo")
%typemap(in, numinputs=0) OBJECT *OUTPUT($1_basetype temp) { $1 = &temp; };
%typemap(in, numinputs=0) OBJECT *OUTPUT($1_basetype temp) %{ $1 = &temp; %}
%typemap(argout) OBJECT *OUTPUT {
PyObject *obj = SWIG_NewPointerObj(memcpy(malloc(sizeof($1_basetype)),
$1,sizeof($1_basetype)),
$descriptor, SWIG_POINTER_NEW);
$result = ($result==NULL) ? obj
: SWIG_Python_AppendOutput($result, obj);
};
}
%apply OBJECT *OUTPUT {
blst_p1 *out, blst_p1 *out_pk, blst_p1 *out_sig,
blst_p1_affine *out, blst_p1_affine *out_pk, blst_p1_affine *out_sig,
blst_p2 *out, blst_p2 *out_pk, blst_p2 *out_sig,
blst_p2_affine *out, blst_p2_affine *out_pk, blst_p2_affine *out_sig,
blst_scalar *out, blst_scalar *out_SK,
blst_fp12 *out
};
}

// accept 'bytes' and 'bytearray' as inputs...
%typemap(in) const byte* {
%typemap(in) const byte* %{
if (PyBytes_Check($input)) {
char *buf;
Py_ssize_t nbytes;
Expand All @@ -55,10 +55,10 @@
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', "
"expecting 'bytes' or 'bytearray'");
}
}
%}
%typemap(freearg) const byte* ""

%typemap(in) const byte[ANY] {
%typemap(in) const byte[ANY] %{
if (PyBytes_Check($input)) {
char *buf;
Py_ssize_t nbytes;
Expand All @@ -79,7 +79,7 @@
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', "
"expecting 'bytes' or 'bytearray'");
}
}
%}
%typemap(freearg) const byte[ANY] ""

%typemap(in) (const byte *STRING, size_t LENGTH) %{
Expand Down Expand Up @@ -144,13 +144,13 @@
%}

#ifdef __cplusplus
%typemap(out) BLST_ERROR {
%typemap(out) BLST_ERROR %{
if ($1 != BLST_SUCCESS) {
SWIG_exception(SWIG_ValueError, BLST_ERROR_str[$1]);
SWIG_fail;
}
$result = SWIG_From_int($1);
};
%}

// return |this|
%typemap(out) SELF* OUTPUT %{ (void)$1; Py_INCREF($result = swig_obj[0]); %}
Expand Down Expand Up @@ -239,7 +239,7 @@
return (Pairing *)ret;
}
~Pairing() { free($self); }
};
}
%typemap(out) blst::Pairing* {
size_t sz = blst_pairing_sizeof();
size_t SZ = (sz + arg2->size() + sizeof(jlong) - 1)/sizeof(jlong);
Expand All @@ -253,7 +253,7 @@
%typemap(throws) BLST_ERROR %{
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException,
BLST_ERROR_str[$1]);
%};
%}

#if SWIG_VERSION < 0x040000
%apply (char *STRING, size_t LENGTH) { (const byte *STRING, size_t LENGTH) }
Expand All @@ -263,11 +263,11 @@
%apply signed char[] { const byte* }
%typemap(in) const byte* %{
$1 = ($1_ltype)JCALL(GetByteArrayElements, $input, 0);
%};
%}
%typemap(argout) const byte* ""
%typemap(freearg) const byte* %{
JCALL(ReleaseByteArrayElements, $input, (jbyte *)$1, JNI_ABORT);
%};
%}

%apply const byte* { const byte[ANY] }
%typemap(in) const byte[ANY] {
Expand All @@ -278,7 +278,7 @@
return $null;
}
$1 = ($1_ltype)JCALL(GetByteArrayElements, $input, 0);
};
}

// let users use 'java.math.BigInteger' as scalars
%typemap(in) (const byte* scalar, size_t nbits) %{
Expand All @@ -299,14 +299,14 @@
if ($1[$2-1] == 0)
$2--;
$2 *= 8;
%};
%}
%typemap(jni) (const byte* scalar, size_t nbits) "jbyteArray"
%typemap(jtype) (const byte* scalar, size_t nbits) "byte[]"
%typemap(jstype) (const byte* scalar, size_t nbits) "java.math.BigInteger"
%typemap(javain) (const byte* scalar, size_t nbits) "$javainput.toByteArray()"

#ifdef __cplusplus
%typemap(javaout) SELF* OUTPUT "{ $jnicall; return this; }"
%typemap(javaout) SELF* OUTPUT { $jnicall; return this; }
#endif

#elif defined(SWIGJAVASCRIPT) && defined(SWIG_JAVASCRIPT_V8)
Expand All @@ -319,9 +319,9 @@
#endif
%}

%typemap(throws) BLST_ERROR %{ SWIG_V8_Raise(BLST_ERROR_str[$1]); SWIG_fail; %};
%typemap(throws) BLST_ERROR %{ SWIG_V8_Raise(BLST_ERROR_str[$1]); SWIG_fail; %}

%typemap(in) const byte* {
%typemap(in) const byte* %{
if ($input->IsArrayBufferView()) {
auto av = v8::Local<v8::ArrayBufferView>::Cast($input);
auto buf = av->Buffer();
Expand All @@ -330,12 +330,12 @@
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', "
"expecting <Buffer>");
}
};
%}
%typemap(argout) const byte* ""
%typemap(freearg) const byte* ""

%apply const byte* { const byte[ANY] }
%typemap(in) const byte[ANY] {
%typemap(in) const byte[ANY] %{
if ($input->IsArrayBufferView()) {
auto av = v8::Local<v8::ArrayBufferView>::Cast($input);
if (av->ByteLength() != $1_dim0)
Expand All @@ -347,7 +347,7 @@
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', "
"expecting <Buffer>");
}
};
%}

// let users use JavaScript <BigInt> and <Buffer> as scalars
%typemap(in) (const byte* scalar, size_t nbits) %{
Expand Down Expand Up @@ -387,9 +387,9 @@
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', "
"expecting <Buffer> or <BigInt>");
}
%};
%}

%typemap(in) (const byte *STRING, size_t LENGTH) {
%typemap(in) (const byte *STRING, size_t LENGTH) %{
if ($input->IsArrayBufferView()) {
auto av = v8::Local<v8::ArrayBufferView>::Cast($input);
auto buf = av->Buffer();
Expand All @@ -404,7 +404,7 @@
SWIG_exception_fail(SWIG_TypeError, "in method '$symname', "
"expecting <Buffer> or <String>");
}
};
%}

// return |this|
%typemap(out) SELF* OUTPUT %{ (void)$1; $result = args.Holder(); %}
Expand All @@ -429,12 +429,10 @@
(const byte *aug, size_t aug_len),
(const byte *IKM, size_t IKM_len),
(const byte *info, size_t info_len)
};
}

// some sorcery to return byte[] from serialization methods
%typemap(in, numinputs=0) byte out[ANY] (byte temp[$1_dim0]) {
$1 = temp;
};
%typemap(in, numinputs=0) byte out[ANY] (byte temp[$1_dim0]) %{ $1 = temp; %}
%typemap(argout) byte out[ANY] {
#if defined(SWIGPYTHON)
PyObject *obj = SWIG_FromCharPtrAndSize((char *)$1, $1_dim0);
Expand All @@ -455,13 +453,13 @@
if ($result == NULL)
$result = SWIG_FromCharPtrAndSize((char *)$1, $1_dim0);
#endif
};
}
%typemap(freearg) byte out[ANY] ""
#ifdef SWIGJAVA
%typemap(jni) byte out[ANY] "jbyteArray"
%typemap(jtype) byte out[ANY] "byte[]"
%typemap(jstype) byte out[ANY] "byte[]"
%typemap(javaout) byte out[ANY] "{ return $jnicall; }"
%typemap(javaout) byte out[ANY] { return $jnicall; }
#endif
%apply byte out[ANY] {
void to_bendian, void blst_bendian_from_scalar,
Expand All @@ -472,10 +470,10 @@
void blst_p2_compress, void blst_p2_affine_compress,
void blst_sk_to_pk2_in_g1, void blst_sign_pk2_in_g1,
void blst_sk_to_pk2_in_g2, void blst_sign_pk2_in_g2
};
}

#ifdef __cplusplus
%apply const std::string& { const std::string* };
%apply const std::string& { const std::string* }

#pragma SWIG nowarn=509,516

Expand All @@ -501,7 +499,7 @@
blst::P1* dbl, blst::P2* dbl,
blst::PT* mul, blst::PT* sqr,
blst::PT* final_exp
};
}

typedef enum {
BLST_SUCCESS = 0,
Expand Down Expand Up @@ -552,7 +550,7 @@ extern const blst::P2_Affine BLS12_381_NEG_G2;
if (dst != NULL) free(dst);
free($self);
}
};
}
#endif

%begin %{
Expand Down

0 comments on commit f2c6230

Please sign in to comment.