Java 10, 137 bytes
(a,b)->{var r=new java.util.Stack();int t=0,i;for(a^=b<a?b^(t=b=a):0;++a<b;){for(i=a;a%--i>0;);if(i<2)r.add(t<1?r.size():0,a);}return r;}
Try it online.
Explanation:
(a,b)->{ // Method with two integer parameters and List return-type
var r=new java.util.Stack();// Result-list, starting empty
int t=0,i; // Temp-integers
for(a^=b<a? // If `b` is smaller than `a`:
b^(t=b=a):0; // Set `t` to `a`,
// and then swap `a` and `b` by using bitwise XORs
++a<b;){ // Loop in the range (a,b):
for(i=a; // Set `i` to the current `a`
a%--i>0;); // Decrease `i` before every iteration with `--i`,
// and continue as long as `a` is NOT divisible by `i`
if(i<2) // If `i` is 1 after the loop (which means `a` is a prime):
r.add(t<1? // If `t` is 0 (which means `a` was already smaller than
// or equal to `b`):
r.size() // Append to the result-list
: // Else (`a` was larger than `b`):
0, // Prepend to the result-list instead
a);} // The current prime `a`
return r;} // And finally return the result-list
Unlike my 05AB1E answer, the exclusive range is actually an advantage for my Java answer, since checking whether a number \$n\geq2\$ is a prime is 3 bytes shorter than checking whether a number \$n\geq1\$ is a prime (see section Primes in this Java tip of mine).
Since I was curious: using an IntStream
is apparently 144 143 bytes:
a->b->java.util.stream.IntStream.iterate(a<b?a+1:a-1,i->b<a?i-1:i+1).limit(a<b?b+~a:a>b?a+~b:0).filter(k->{int i=k;for(;k%--i>0;);return i<2;})
-1 byte thanks to @Neil.
Try it online.
Explanation:
a->b-> // Method with two integer parameters and IntStream return
java.util.stream.IntStream // Create an IntStream
.iterate(a<b? // If `a` is smaller than `b`
a+1 // Start at `a+1`
: // Else (a>=b):
a-1, // Start at `a-1` instead
i-> // In every iteration:
b<a? // If `b` is smaller than `a`:
i-1 // Decrement once
: // Else (b>=a)
i+1) // Increment once instead
.limit(a<b? // If `a` is smaller than `b`:
b+~a // Do `b-a-1` amount of iterations
:a>b? // Else-if `a` is larger than `b`:
a+~b // Do `a-b-1` amount of iterations
: // Else (`a` equals `b`):
0) // Make the IntStream empty
.filter(k->{ // Then filter this IntStream by:
int i=k;for(;k%--i>0;);return i<2;})
// Primes-check similar as above