Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change strict mode. #3

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ my %args = (
},

requires => {
'Devel::StrictMode' => '0.003',
'XS::Parse::Keyword' => '0.36',
'perl' => '5.016000',
},
Expand Down
1 change: 0 additions & 1 deletion META.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
},
"runtime" : {
"requires" : {
"Devel::StrictMode" : "0.003",
"XS::Parse::Keyword" : "0.36",
"perl" : "5.016000"
}
Expand Down
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Syntax::Keyword::Assert introduces a lightweight assert keyword to Perl, designe

- **STRICT Mode**

When STRICT mode is enabled, assert statements are checked at runtime. If the assertion fails (i.e., the block returns false), the program dies with an error. This is particularly useful for catching errors during development or testing.
When STRICT mode is enabled, assert statements are checked at runtime. Default is enabled. If the assertion fails (i.e., the block returns false), the program dies with an error. This is particularly useful for catching errors during development or testing.

- **Zero Runtime Cost**

Expand All @@ -35,26 +35,14 @@ Syntax::Keyword::Assert introduces a lightweight assert keyword to Perl, designe

## STRICT Mode Control

The behavior of STRICT mode is controlled by the [Devel::StrictMode](https://metacpan.org/pod/Devel%3A%3AStrictMode) module. You can enable or disable STRICT mode depending on your environment (e.g., development, testing, production).

For example, to enable STRICT mode:
If `$ENV{PERL_ASSERT_ENABLED}` is trusy, STRICT mode is enabled. Otherwise, it is disabled. Default is enabled.

```perl
BEGIN { $ENV{PERL_STRICT} = 1 } # Enable STRICT mode
BEGIN { $ENV{PERL_ASSERT_ENABLED} = 0 } # Disable STRICT mode

use Syntax::Keyword::Assert;
use Devel::StrictMode;

assert { 1 == 1 }; # Always passes
assert { 0 == 1 }; # Dies if STRICT mode is enabled
```

To disable STRICT mode (it is disabled by default):

```perl
use Syntax::Keyword::Assert;
use Devel::StrictMode;

assert { 0 == 1 }; # Block is ignored, no runtime cost
```

Expand Down
6 changes: 1 addition & 5 deletions bench/compare-no-assertion.pl
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ =head1 RESULT
use Benchmark qw(cmpthese);

BEGIN {
# Disable STRICT mode. SEE ALSO: Devel::StrictMode
$ENV{EXTENDED_TESTING} = 0;
$ENV{AUTHOR_TESTING} = 0;
$ENV{RELEASE_TESTING} = 0;
$ENV{PERL_STRICT} = 0;
$ENV{PERL_ASSERT_ENABLED} = 0;
}

use Syntax::Keyword::Assert;
Expand Down
1 change: 0 additions & 1 deletion cpanfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
requires 'perl', '5.016000';
requires 'XS::Parse::Keyword' => '0.36';
requires 'Devel::StrictMode', => '0.003';

on 'configure' => sub {
requires 'Module::Build' => '0.4004';
Expand Down
17 changes: 3 additions & 14 deletions lib/Syntax/Keyword/Assert.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use v5.14;
use warnings;

use Carp ();
use Devel::StrictMode;

require XSLoader;
XSLoader::load( __PACKAGE__, our $VERSION );
Expand Down Expand Up @@ -72,7 +71,7 @@ Syntax::Keyword::Assert introduces a lightweight assert keyword to Perl, designe

=item B<STRICT Mode>

When STRICT mode is enabled, assert statements are checked at runtime. If the assertion fails (i.e., the block returns false), the program dies with an error. This is particularly useful for catching errors during development or testing.
When STRICT mode is enabled, assert statements are checked at runtime. Default is enabled. If the assertion fails (i.e., the block returns false), the program dies with an error. This is particularly useful for catching errors during development or testing.

=item B<Zero Runtime Cost>

Expand All @@ -86,23 +85,13 @@ The syntax is straightforward—assert BLOCK—making it easy to integrate into

=head2 STRICT Mode Control

The behavior of STRICT mode is controlled by the L<Devel::StrictMode> module. You can enable or disable STRICT mode depending on your environment (e.g., development, testing, production).
If C<$ENV{PERL_ASSERT_ENABLED}> is trusy, STRICT mode is enabled. Otherwise, it is disabled. Default is enabled.

For example, to enable STRICT mode:

BEGIN { $ENV{PERL_STRICT} = 1 } # Enable STRICT mode
BEGIN { $ENV{PERL_ASSERT_ENABLED} = 0 } # Disable STRICT mode

use Syntax::Keyword::Assert;
use Devel::StrictMode;

assert { 1 == 1 }; # Always passes
assert { 0 == 1 }; # Dies if STRICT mode is enabled

To disable STRICT mode (it is disabled by default):

use Syntax::Keyword::Assert;
use Devel::StrictMode;

assert { 0 == 1 }; # Block is ignored, no runtime cost

SEE ALSO:
Expand Down
38 changes: 15 additions & 23 deletions lib/Syntax/Keyword/Assert.xs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,7 @@

#include "XSParseKeyword.h"

static bool is_strict(pTHX)
{
dSP;

ENTER;
SAVETMPS;

PUSHMARK(SP);
call_pv("Syntax::Keyword::Assert::STRICT", G_SCALAR);
SPAGAIN;

bool ok = SvTRUEx(POPs);

PUTBACK;

FREETMPS;
LEAVE;

return ok;
}
static bool assert_enabled = TRUE;

static OP *make_xcroak(pTHX_ OP *msg)
{
Expand All @@ -39,22 +20,23 @@ static OP *make_xcroak(pTHX_ OP *msg)

static int build_assert(pTHX_ OP **out, XSParseKeywordPiece *arg0, void *hookdata)
{
if (is_strict(aTHX)) {
OP *argop = arg0->op;
if (assert_enabled) {
// build the following code:
//
// Syntax::Keyword::Assert::_croak "Assertion failed"
// unless do { $a == 1 }
//
OP *block = arg0->op;
OP *msg = newSVOP(OP_CONST, 0, newSVpvs("Assertion failed"));

*out = newLOGOP(OP_AND, 0,
newUNOP(OP_NOT, 0, block),
newUNOP(OP_NOT, 0, argop),
make_xcroak(aTHX_ msg)
);
}
else {
// do nothing.
op_free(argop);
*out = newOP(OP_NULL, 0);
}

Expand All @@ -72,3 +54,13 @@ MODULE = Syntax::Keyword::Assert PACKAGE = Syntax::Keyword::Assert
BOOT:
boot_xs_parse_keyword(0.36);
register_xs_parse_keyword("assert", &hooks_assert, NULL);

{
const char *enabledstr = getenv("PERL_ASSERT_ENABLED");
if(enabledstr) {
SV *sv = newSVpvn(enabledstr, strlen(enabledstr));
if(!SvTRUE(sv))
assert_enabled = FALSE;
}
}

6 changes: 1 addition & 5 deletions t/01_assert.t
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use Test2::V0;

BEGIN {
$ENV{PERL_STRICT} = 1;
}

use Syntax::Keyword::Assert;

subtest 'Test `assert` keyword with STRICT enabled' => sub {
subtest 'Test `assert` keyword' => sub {
like dies {
assert { 0 };
}, qr/\AAssertion failed/;
Expand Down
8 changes: 2 additions & 6 deletions t/02_assert_with_strict_disabled.t → t/02_assert_disabled.t
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
use Test2::V0;

BEGIN {
# Disable STRICT mode. SEE ALSO: Devel::StrictMode
$ENV{EXTENDED_TESTING} = 0;
$ENV{AUTHOR_TESTING} = 0;
$ENV{RELEASE_TESTING} = 0;
$ENV{PERL_STRICT} = 0;
$ENV{PERL_ASSERT_ENABLED} = !!0
}

use Syntax::Keyword::Assert;

subtest 'Test `assert` keyword with STRICT disabled' => sub {
subtest 'Test `assert` keyword when $ENV{PERL_ASSERT_ENABLED} is falsy' => sub {
ok lives {
assert { 0 };
};
Expand Down
Loading